首页 > 下载 > 下载详文:WPF TreeView动态指定选取节点

WPF TreeView动态指定选取节点

发布时间:2012年08月09日 11时11分05秒   属性:程序Web开发 > Microsoft    访问次数:90050
字体: 初始 添加收藏 分享给好友

WPF TreeView动态指定选取节点

WPF TreeView节点操作控制,实现动态查询展开任意某个节点。在前面WPF的TreeView数据绑定基础WPF的TreeView从数据库绑定数据WPF的ComboBox关联数据绑定 几篇文章中都已经对TreeView的节点数据原理进行详细示例描述,同时也做了详细数据库结构剖析。在本文也可以说对前面几篇文章的知识综合到一起来,实现ComboBox绑定数据关联操作TreeView节点展开选取操作。本文不会对TreeView、Combobox控件的基础介绍,以及控件数据绑定数据库原理也不会详细介绍,可以参考前面几篇文章。下面是程序执行效果,当ComboBox选取到某项时,TreeView就关联对应自动展开选取到所属节点上。

现在MainWindow.xaml主窗体中继续WPF的ComboBox关联数据绑定文章中做下调整加入两个ComboBox(在本文实例中就按默认Id,comboBox1和comboBox2),一个用来绑定主城市,一个用来关联绑定城市分区子级,这里在webservice的CityService程序中同样加 Citylist()和CityItem(int id),在“WPF的ComboBox关联数据绑定”是通过WCF程序CityWCFService同样调用的Citylist()和CityItem(int id)。然后再两个ComboBox的SelectionChanged事件中都触发 
ExpandTree(treeView, ((CityInfo)comboBox1.SelectedItem).CityId, ((CityInfo)
comboBox2.SelectedItem).CityItmeId);
即根据ComboBox选取城市项的Id来关联TreeView节点,下面是核心代码。

C# 代码  复制
/// <summary> /// WPF TreeView动态指定选取节点 /// Copyright (C) 遗昕 | weisim3.com 08.09.2012 /// </summary> /// <param name="treeView">treeView -> TreeView控件/TreeView Control</param> /// <param name="cityId">cityId -> 主城市Id(父级)/Parent City Id</param> /// <param name="cityItemId">cityItemId -> 子城区Id(子级) - Child City Id</param> public void ExpandTree(TreeView treeView,int cityId,int cityItemId) { foreach (object item in treeView.Items)//ws.Citylist() { if (((CityInfo)item).CityId == cityId) { TreeViewItem parentViewItem = (TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(item); parentViewItem.IsSelected = true; parentViewItem.IsExpanded = true; parentViewItem.UpdateLayout(); foreach (object data in parentViewItem.Items) { if (((CityInfo)data).CityItmeId == cityItemId) { try { TreeViewItem TreeViewItem = (TreeViewItem)parentViewItem.ItemContainerGenerator. ContainerFromItem(data); TreeViewItem.IsSelected = true; TreeViewItem.IsExpanded = true; TreeViewItem.BringIntoView(); } catch { } } } } } }

上面代码可以看到foreach循环了两次,先将TreeView主节点循环出来,也就是主城市名循环出来,通过ItemContainerGenerator.ContainerFromItem来拾取城市。然后再次将二级子级循环出来。在每个节点循环之后都需要将IsExpanded赋值为true和IsSelected赋值为true,即展开节点和选取节点。在第一个节点循环展开时需要追加UpdateLayout()方法用来确保此元素的所有可视子元素都正确地进行了布局更新。在子节点中追加入BringIntoView()方法用来尝试将此元素放入其所在的任何可滚动区域内的视图中。

将ExpandTree(TreeView treeView,int cityId,int cityItemId)放在两个ComboBox的SelectionChanged事件中加入,显示详细ComboBox的SelectionChanged相应代码。

C# 代码  复制
private void comboBox1_SelectionChanged(object sender,
SelectionChangedEventArgs e) { //WPF TreeView动态指定选取节点 //Copyright (C) 遗昕 | weisim3.com 08.09.2012 if (Convert.ToInt32(comboBox1.SelectedValue) == 0) { List<CityInfo> list = new List<CityInfo>(); list.Add(new CityInfo() { CityItmeId = 0, CityItmeName = "-城市分区-" }); comboBox2.ItemsSource = list; comboBox2.DisplayMemberPath = "CityItmeName"; comboBox2.SelectedValuePath = "CityItmeId"; comboBox2.SelectedValue = 0; //comboBox2.Items.Add("-城市分区-"); //comboBox2.SelectedValue = "-城市分区-"; } else { comboBox2.ItemsSource =
ws.CityItem(Convert.ToInt32(comboBox1.SelectedValue)); comboBox2.DisplayMemberPath = "CityItmeName"; comboBox2.SelectedValuePath = "CityItmeId"; comboBox2.SelectedValue = 0; } treeView.ItemsSource = ws.Citylist(); if (Convert.ToInt32(comboBox1.SelectedValue) > 0) { ExpandTree(treeView, ((CityInfo)comboBox1.SelectedItem).CityId, 0); } } private void comboBox2_SelectionChanged(object sender,
SelectionChangedEventArgs e) { //WPF TreeView动态指定选取节点 //Copyright (C) 遗昕 | weisim3.com 08.09.2012 if (Convert.ToInt32(comboBox2.SelectedValue) > 0) { treeView.ItemsSource = ws.Citylist(); ExpandTree(treeView, ((CityInfo)comboBox1.SelectedItem).CityId, ((CityInfo)comboBox2.SelectedItem).CityItmeId); try { label1.Content = ((CityInfo)comboBox1.SelectedItem).CityName + ">>>" + ((CityInfo)comboBox2.SelectedItem).CityItmeName; } catch { } } }

通过这样来完成TreeView节点动态指定,自动智能检索指定到某个节点的数据,通过WPF的TreeView数据绑定基础WPF的TreeView从数据库绑定数据WPF的ComboBox关联数据绑定这几篇文章的连续介绍,已经深入到TreeView和ComboBox的后台数据绑定原理。其他细节逻辑判断原理,或细节基础需要读者用户自己去深入细节了解熟悉,知识思路使用就介绍到这里。

WPF TreeView动态指定选取节点 (30)
本下载连接不支持第三下载工具打开,请直接点击下载即可
文章版权归属weisim3.com所有,未经书面版权许可同意,不得私自转载(或做修改转载),源文件示例仅供学习使用,更不要出于商业用途或印刷出版发行!否则将追究其相关法律责任,版权联系QQ:729260499。
遺昕 | Weisim3.com 下载许可条款 ( 您本次需要付费下载 ) .



付费源文件: WPF TreeView动态指定选取节点
支付金额: ¥20.00        授权期限: 10
5TwjdQC7id4Z1YJIolhTHWYLhgQ9Ki -- 20240329132253