wpf绑定TreeView,貌似比winForm可能要麻烦一些,不过仔细分析一下,还是很好解决的,最近做了一个项目,正好写了一下,下面把代码贴出来和大家分享一下
首先、需要创建一个节点类,包括节点名称,图片,内容等,另外为了可以增加子节点,在加一个泛型IList,类型就是节点类本身
/// <summary> /// 节点类 /// </summary> internal class PropertyNodeItem { public string Icon { get; set; } public string EditIcon { get; set; } public string DisplayName { get; set; } public string Name { get; set; } public int id { get; set; } public int parentId{get;set;} public bool IsExpanded { get; set; } public List<PropertyNodeItem> Children { get; set; } public PropertyNodeItem() { Children = new List<PropertyNodeItem>(); } }
//根节点添加到treeVIew中 private void loadTree() { tvProperties.Items.Clear();//加载根节点前先清除Treeview控件项 List<PropertyNodeItem> itemList = new List<PropertyNodeItem>(); DataTable dt=webDict.GetArticles(); DataView dv = dt.DefaultView; dv.RowFilter = " ClassType='3' and ParentId=0 "; PropertyNodeItem node = new PropertyNodeItem() { DisplayName = dv[0].Row["title"].ToString(), Name = dv[0].Row["title"].ToString(), id=Convert.ToInt32(dv[0].Row["id"].ToString()), parentId=Convert.ToInt32(dv[0].Row["ParentId"].ToString()), IsExpanded=true }; int id =Convert.ToInt32(dv[0]["id"].ToString()); int pid = Convert.ToInt32(dv[0]["ParentId"].ToString()); ForeachPropertyNode(node, id); itemList.Add(node); this.tvProperties.ItemsSource = itemList; }
//无限接循环子节点添加到根节点下面 private void ForeachPropertyNode(PropertyNodeItem node,int pid) { DataTable dtDict = webDict.GetArticles(); DataView dvDict = dtDict.DefaultView; dvDict.RowFilter = " ClassType='3' and ParentId=" + pid; if (dvDict.Count > 0) { foreach (DataRowView view in dvDict) { int id = Convert.ToInt32(view["id"].ToString()); string name = view["title"].ToString(); int parentId = Convert.ToInt32(view["ParentId"].ToString()); PropertyNodeItem childNodeItem = new PropertyNodeItem() { DisplayName = name, Name = name, id=id, parentId=parentId, IsExpanded=false }; ForeachPropertyNode(childNodeItem, id); node.Children.Add(childNodeItem); } } }
//选中节点事件 private void tvProperties_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { PropertyNodeItem item = this.tvProperties.SelectedItem as PropertyNodeItem; string nodeText= item.Name; // MessageBox.Show(nodeText); }
<TreeView Height="264" x:Name="tvProperties" Width="220" SelectedItemChanged="tvProperties_SelectedItemChanged"> <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpanded" Value="{Binding IsExpanded}"></Setter> </Style> </TreeView.ItemContainerStyle> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}"> <StackPanel Orientation="Horizontal"> <Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16" Height="16" Margin="0,0,2,2"></Image> <TextBlock VerticalAlignment="Center" Name="nodeName" Text="{Binding DisplayName}"></TextBlock> <Image VerticalAlignment="Center" Source="{Binding EditIcon}" Margin="2,0,0,0"></Image> <StackPanel.ToolTip> <TextBlock VerticalAlignment="Center" Text="{Binding Name}" TextWrapping="Wrap" MaxWidth="200" ></TextBlock> </StackPanel.ToolTip> </StackPanel> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>