//遍历搜索查找的方式 bool SetSelect(TreeListViewItemViewModel data, string selFullName) { bool isSel = false; foreach (TreeListViewItemViewModel chd in data.Children) { if (chd.FullPath == selFullName) { chd.IsSelected = true; return(true); } chd.IsExpanded = true;//展开子节点(可能加载子项) // 按需延时装载子项 if (chd.HasDummyChild) { AddSubNode(chd.DataRowView[Key].ToString(), chd);//加载子节点 } isSel = SetSelect(chd, selFullName); if (isSel) { return(true); } } return(isSel); }
/// <summary> /// 清空所有的数据对象,释放资源 /// </summary> public void Clear() { _SelectedItem = null; _TreeRoot.Children.Clear(); AfterTreeListItemSelected = null; _TreeRoot = null; _DataSource = null; }
public TreeListDataTable(DataView dtable, string parentKey, string key, bool isUseDummyChild = true, string isDummyChildField = "") { ParentKey = parentKey; Key = key; IsDummyChildField = isDummyChildField; //创建用于绑定的数据源 _TreeRoot = new TreeListViewItemViewModel(); //设定是否使用延迟加载项 IsUseDummyChild = isUseDummyChild; _DataSource = dtable; //记录数据对象 FillData(); //填充数据 }
//项被选择后执行 void TreeListItemSelected(TreeListViewItemViewModel item) { //设定选择的全称路径 if (_SelectedFullPath != item.FullPath) { _SelectedFullPath = item.FullPath; this.OnPropertyChanged(() => this.SelectedFullPath); _SelectedItem = item; this.OnPropertyChanged(() => this.SelectedItem); } //调用外部委托 AfterTreeListItemSelected(item); }
//通过路径解析直接定位,速度更快 bool SetSelected(TreeListViewItemViewModel parentTreeList, string selFullName, ref string currSubFullName) { string[] keys = currSubFullName.Split(new char[1] { '.' }); //使用登号分隔符 if (keys.Length < 1) { return(false); } // 按需延时装载子项 if (parentTreeList.HasDummyChild) { AddSubNode(parentTreeList.DataRowView[Key].ToString(), parentTreeList);//加载子节点 } //获取子键 string subKey = keys[0]; bool isSel = false; foreach (TreeListViewItemViewModel chd in parentTreeList.Children) { if (chd.DataRowView[Key].ToString() == subKey) //搜寻项为当前项或者其内部的子项 { chd.Parent.IsExpanded = true; //当前项的父亲展开 if (chd.FullPath == selFullName) //搜索为当前项 { chd.IsSelected = true; return(true); } else //搜索子项 { //截取子项 if (subKey.Length >= currSubFullName.Length) { return(false); } currSubFullName = currSubFullName.Substring(subKey.Length + 1); isSel = SetSelected(chd, selFullName, ref currSubFullName); if (isSel) { return(true); } } } } return(isSel); }
static void SetIndexPos(TreeListViewItemViewModel treeList, ref int idx) { if (treeList.Children == null) { return; } foreach (TreeListViewItemViewModel sunbItem in treeList.Children) { if (idx < 1) { sunbItem.IndexPos = 1; idx = 1; } else { sunbItem.IndexPos = 0; idx = 0; } SetIndexPos(sunbItem, ref idx); } }
//展开时加载数据项 void AfterNodeIsExpanded(TreeListViewItemViewModel node) { DataRowView dt = node.DataRowView;//当前数据项 AddSubNode(dt[Key].ToString(), node); }
//添加子节点数据 void AddSubNode(string parentKey, TreeListViewItemViewModel parentNode) { if (_DataSource == null) { return; } if (ParentKey == "" || Key == "") { throw new NotSupportedException("未指定ParentKey或者Key属性值"); } _DataSource.RowFilter = ParentKey + " = '" + parentKey + "'";//获取父节点下的子节点 //添加子节点到集合中 foreach (DataRowView rowView in _DataSource) { TreeListViewItemViewModel node = null; bool isDummy = false; //是否使用动态延迟加载 //创建子节点 if (!IsUseDummyChild) //不使用延迟加载且为根节点 { node = new TreeListViewItemViewModel(rowView); node.AfterTreeListItemSelected = TreeListItemSelected; if (parentNode.FullPath == "") { node.FullPath = rowView[Key].ToString(); } else { node.FullPath = parentNode.FullPath + "." + rowView[Key];//记录用于定位的完整路径信息 } //添加到父节点集合 parentNode.Children.Add(node); //不使用延迟加载时,添加所有的子项 AddSubNode(rowView[Key].ToString(), node); } else //在主开关使用了延迟加载后,通过字段控制延迟加载可以保证最底层的树不显示延迟加载 { if (IsDummyChildField == "") //未指定加载字段,则由主开关来确定使用延迟加载 { node = new TreeListViewItemViewModel(rowView, true); node.AfterTreeListItemSelected = TreeListItemSelected; node.AfterListItemIsExpanded = AfterNodeIsExpanded; if (parentNode.FullPath == "") { node.FullPath = rowView[Key].ToString(); } else { node.FullPath = parentNode.FullPath + "." + rowView[Key];//记录用于定位的完整路径信息 } //添加到父节点集合 parentNode.Children.Add(node); } else { if (rowView[IsDummyChildField] is DBNull) { isDummy = true; } else { isDummy = System.Convert.ToBoolean(rowView[IsDummyChildField]); } if (isDummy) { node = new TreeListViewItemViewModel(rowView, true); node.AfterTreeListItemSelected = TreeListItemSelected; node.AfterListItemIsExpanded = AfterNodeIsExpanded; if (parentNode.FullPath == "") { node.FullPath = rowView[Key].ToString(); } else { node.FullPath = parentNode.FullPath + "." + rowView[Key];//记录用于定位的完整路径信息 } //添加到父节点集合 parentNode.Children.Add(node); } else //不使用延迟加载 { node = new TreeListViewItemViewModel(rowView); node.AfterTreeListItemSelected = TreeListItemSelected; if (parentNode.FullPath == "") { node.FullPath = rowView[Key].ToString(); } else { node.FullPath = parentNode.FullPath + "." + rowView[Key];//记录用于定位的完整路径信息 } //添加到父节点集合 parentNode.Children.Add(node); //为了提高性能,默认为是最内层的数据,不会再添加子项 //AddSubNode(rowView[Key].ToString(), node); } } } } }
/// <summary> /// 设置项的位置索引 /// </summary> /// <param name="treeList"></param> public static void SetIndexPos(TreeListViewItemViewModel treeList) { int idx = 1; SetIndexPos(treeList, ref idx); }