在wpf中实现treeview的功能,可能看到很多分享的都是简单的绑定,仅此记录自己完成的功能。
前台
<TreeView x:Name="chapterTree" Grid.Column="0" SelectedItemChanged="chapterTree_SelectedItemChanged" PreviewMouseRightButtonDown="chapterTree_PreviewMouseRightButtonDown" FontSize="14" ItemsSource="{Binding Path=Nodes}" > <TreeView.ContextMenu> <ContextMenu> <MenuItem Header="保存" Name="btnAlterData" Click="btnAlterData_Click" /> <MenuItem Header="新增兄弟节点" Name="btnSaveData" Click="btnSaveData_Click"/> <MenuItem Header="新增子节点" Name="btnSaveSonData" Click="btnSaveSonData_Click"/> <MenuItem Header="删除" Name="btnDdeleteData" Click="btnDdeleteData_Click"/> <MenuItem Header="上移一位" Name="btnUp" Click="btnUp_Click"/> <MenuItem Header="下移一位" Name="btnDown" Click="btnDown_Click"/> </ContextMenu> </TreeView.ContextMenu> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type c:OrderModel}" ItemsSource="{Binding Path=ChildNodes}"> <Label Content="{Binding Path=OrderName}"/> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
后台
CommandModel cm; public Command() { InitializeComponent(); cm = new CommandModel(); chapterTree.ItemsSource = cm.Nodes; }
新建了CommandModel页面用来绑定数据(从数据库提取的数据,转换成了list)
public event PropertyChangedEventHandler PropertyChanged; private ObservableCollection<OrderModel> nodes = new ObservableCollection<OrderModel>(); public ObservableCollection<OrderModel> Nodes { get { return nodes; } set { nodes = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Nodes")); } } public CommandModel() { Initiate(); } private void Initiate() { using (SQLiteConnection con = new SQLiteConnection(MainWindow.sb.ToString())) { DataTable table = new DataTable("MyTable"); var reader = con.ExecuteReader("SELECT * FROM OrderData Order by OrderID"); table.Load(reader); ObservableCollection<OrderModel> _nodes = new ObservableCollection<OrderModel>(); _nodes = ConvertToList(table); //ShowTreeView(); Nodes = getChildNodes(0, _nodes); } } private ObservableCollection<OrderModel> getChildNodes(long parentID, ObservableCollection<OrderModel> nodes) { List<OrderModel> a = new List<OrderModel>(nodes.Where(x => x.ParentID == parentID).ToList()); ObservableCollection<OrderModel> mainNodes =new ObservableCollection<OrderModel>(a); List<OrderModel> b = new List<OrderModel>(nodes.Where(x => x.ParentID != parentID).ToList()); ObservableCollection<OrderModel> otherNodes =new ObservableCollection<OrderModel>(b); foreach (OrderModel node in mainNodes) node.ChildNodes = getChildNodes(node.ID, otherNodes); return mainNodes; } #region datatable转list /// <summary> /// datatable转list /// </summary> /// <param name="dt"></param> /// <returns></returns> public static ObservableCollection<OrderModel> ConvertToList(DataTable dt) { // 定义集合 ObservableCollection<OrderModel> ts = new ObservableCollection<OrderModel>(); // 获得此模型的类型 Type type = typeof(OrderModel); //定义一个临时变量 string tempName = string.Empty; //遍历DataTable中所有的数据行 foreach (DataRow dr in dt.Rows) { OrderModel t = new OrderModel(); // 获得此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); //遍历该对象的所有属性 foreach (PropertyInfo pi in propertys) { tempName = pi.Name;//将属性名称赋值给临时变量 //检查DataTable是否包含此列(列名==对象的属性名) if (dt.Columns.Contains(tempName)) { // 判断此属性是否有Setter if (!pi.CanWrite) continue;//该属性不可写,直接跳出 //取值 object value = dr[tempName]; //如果非空,则赋给对象的属性 if (value != DBNull.Value) pi.SetValue(t, value, null); } } //对象添加到泛型集合中 ts.Add(t); } return ts; } #endregion
model页面的列中需要添加
public ObservableCollection<OrderModel> ChildNodes { get; set; } public OrderModel() { ChildNodes = new ObservableCollection<OrderModel>(); }