WPF TreeView绑定数据
1、前台代码
<TreeView Name="tvOrg" HorizontalAlignment="Left" Width="210" SelectedItemChanged="tvOrg_SelectedItemChanged" > <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsExpanded" Value="True"></Setter> </Style> </TreeView.ItemContainerStyle> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}"> <StackPanel Orientation="Horizontal"> <Image VerticalAlignment="Center" Source="/Images/WinFolder.gif" Width="16" Height="16" Margin="0,0,2,2"></Image> <TextBlock VerticalAlignment="Center" Name="nodeName" Text="{Binding Name}"></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.ContextMenu><!--右击菜单--> <ContextMenu> <MenuItem Name="tvcmAdd" Header="添加" Click="btnAddOrg_Click"> <!--<MenuItem.Template> <ControlTemplate> <Image Source="/Images/AppIcon.ico" Width="16" Height="16"></Image> </ControlTemplate> </MenuItem.Template>--> </MenuItem> <MenuItem Name="tvmEdit" Header="编辑" Click="btnEditOrg_Click"></MenuItem> <MenuItem Name="tvcmDelete" Header="删除" Click="btnDelOrg_Click"/> </ContextMenu> </TreeView.ContextMenu> </TreeView>
2、数据获取
1)数据载体
public class PropertyNodeItem { public string Icon { get; set; } public string EditIcon { get; set; } public string DisplayName { get; set; } public string Name { get; set; } public long id { get; set; } public Guid? OrgGuid { get; set; } public Guid? OrgParentGuid { get; set; } public long parentId { get; set; } public bool IsExpanded { get; set; } public int SortIndex { get; set; } public List<PropertyNodeItem> Children { get; set; } public PropertyNodeItem() { Children = new List<PropertyNodeItem>(); } }
2)处理数据
1 /// <summary> 2 /// 赋值给Node集合 3 /// </summary> 4 /// <param name="dt"></param> 5 public List<PropertyNodeItem> Transform() 6 { 7 DataTable dt = sysOrganization.LoadOrgData().Tables[0]; 8 List<PropertyNodeItem> listNode = new List<PropertyNodeItem>(); 9 10 listNode.Add(new PropertyNodeItem 11 { 12 Name = "组织机构", 13 parentId = -1, 14 id = 0, 15 SortIndex = 0 16 }); 17 18 foreach (DataRow dr in dt.Rows) 19 { 20 PropertyNodeItem temp = ConvertToTableModel(dr); 21 //赋值 22 listNode.Add(temp); 23 } 24 25 List<PropertyNodeItem> outputList = Bind(listNode); 26 //List<PropertyOrgNode> result = new List<PropertyOrgNode>(); 27 28 //foreach (var item in outputList.OrderBy(p => p.SortIndex)) 29 //{ 30 // PropertyOrgNode temp = new PropertyOrgNode(); 31 // temp.Item = item; 32 // result.Add(temp); 33 //} 34 //TreeViewtest1.ItemsSource = outputList; 35 return outputList.OrderBy(p => p.SortIndex).ToList(); 36 } 37 38 /// <summary> 39 /// 赋值给Node 40 /// </summary> 41 /// <param name="dr"></param> 42 /// <returns></returns> 43 public static PropertyNodeItem ConvertToTableModel(DataRow dr) 44 { 45 PropertyNodeItem result = new PropertyNodeItem(); 46 if (false == DBNull.Value.Equals(dr["OrgID"].ToString())) result.id = Int64.Parse(dr["OrgID"].ToString()); 47 if (false == DBNull.Value.Equals(dr["OrgName"])) result.Name = dr["OrgName"].ToString(); 48 if (false == DBNull.Value.Equals(dr["OrgParentID"])) result.parentId = Int64.Parse(dr["OrgParentID"].ToString()); 49 if (false == DBNull.Value.Equals(dr["SortIndex"])) result.SortIndex = Int32.Parse(dr["SortIndex"].ToString()); 50 return result; 51 } 52 53 /// <summary> 54 /// 绑定树 55 /// </summary> 56 List<PropertyNodeItem> Bind(List<PropertyNodeItem> nodes) 57 { 58 //定义一个返回值变量 59 List<PropertyNodeItem> outputList = new List<PropertyNodeItem>(); 60 61 //循环遍历控件 62 for (int i = 0; i < nodes.Count; i++) 63 { 64 if (nodes[i].parentId == -1) outputList.Add(nodes[i]);//第一次调用时,ParentID为-1 也就是父级 65 66 else FindDownward(nodes, nodes[i].parentId).Children.Add(nodes[i]); 67 } 68 return outputList; 69 } 70 /// <summary> 71 /// 向下查找 72 /// </summary> 73 /// <param name="nodes"></param> 74 /// <param name="id"></param> 75 /// <returns></returns> 76 PropertyNodeItem FindDownward(List<PropertyNodeItem> nodes, long id) 77 { 78 if (nodes == null) return null; 79 for (int i = 0; i < nodes.Count; i++) 80 { 81 //寻找子节点 82 if (nodes[i].id == id) 83 return nodes[i]; 84 85 86 //判断NodeNew是否为空,在这里NodeNew不会为空,所以可不用 87 PropertyNodeItem node = FindDownward(nodes[i].Children, id); 88 if (node != null) return node; 89 } 90 return null; 91 }
3、数据绑定
private void BindToTree() { List<PropertyNodeItem> result = sysOrgBLL.Transform(); this.tvOrg.ItemsSource = result; }
浙公网安备 33010602011771号