/// <summary> /// 节点选中后的事件 /// </summary> /// User:Ryan CreateTime:2012-9-28 15:42. void PopDownTree_AfterCheck(object sender, TreeViewEventArgs e) { TreeNodeEx node = e.Node as TreeNodeEx; if (node != null) { if (node is DefaultTreeNode) { return; } else { //add if (node.Checked) { this._CheckedNodes.Add(node); } //remove else { this._CheckedNodes.Remove(node); } //update text this.UpdateText(node); } } }
/// <summary> /// 绑定一级节点 /// </summary> /// <param name="item">The item.</param> /// User:Ryan CreateTime:2012-9-27 17:39. protected virtual void BindRootNode(object item) { TreeNodeEx node = this.GetRootNode(item); this.PopDownTree.Nodes.Add(node); //绑定子节点 this.BindChildNode(item, node); }
/// <summary> /// 获取指定接的的全部值 /// </summary> /// User:Ryan CreateTime:2012-9-27 19:25. private List <string> GetFullPathValues(TreeNodeEx node) { List <string> list = new List <string>(); list.Add(node.Value); if (node.Parent != null) { list.AddRange(this.GetFullPathValues(node.Parent as TreeNodeEx)); } return(list); }
/// <summary> /// 绑定子节点(自反射数据源) /// </summary> /// <param name="item">The item.</param> /// <param name="node">The node.</param> /// User:Ryan CreateTime:2012-9-27 17:39. protected virtual void BindChildNode(object item, TreeNodeEx node) { //绑定多级节点 if (!string.IsNullOrEmpty(this.MultiLevelDataSourceMember)) { object dataSource = item.TryGetValue(this.MultiLevelDataSourceMember); if (dataSource != null) { this.BindChildNode(node, dataSource); } } }
/// <summary> /// 设置当即选中树节点 /// </summary> /// <param name="node">The node.</param> /// User:Ryan CreateTime:2012-9-27 20:14. private void SetSelectedNode(TreeNodeEx node) { this.UpdateText(node); if (node is DefaultTreeNode) { this.SelectedNode = null; } else { this.SelectedNode = node; if (this.OnTreeNodeSelected != null) { TreeNodeEventArgs args = new TreeNodeEventArgs(); args.SelectedNode = this.SelectedNode; this.OnTreeNodeSelected(this.PopDownTree, args); } } }
/// <summary> /// 树节点展开处理事件 /// </summary> /// <param name="sender">(控件对象).The source of the event.</param> /// <param name="e">(事件数据).The <see cref="System.Windows.Forms.TreeViewCancelEventArgs"/> instance containing the event data.</param> /// User:Ryan CreateTime:2012-9-27 18:50. void PopDownTree_BeforeExpand(object sender, TreeViewCancelEventArgs e) { TreeNodeEx node = e.Node as TreeNodeEx; if (!node.LeafNode && node.Datasource == null && node.Nodes[0] != null && node.Nodes[0] is EmptyTreeNode) { //clear data node.LeafNode = true; node.Nodes.Clear(); //实现该事件以加载子节点数据 if (this.LoadChildNodesDataSource != null) { TreeNodeEventArgs args = new TreeNodeEventArgs(); args.SelectedNode = node; this.LoadChildNodesDataSource(sender, args); //bind data this.BindChildNode(node, args.DataSource); } } }
private void SetCheckedNodeByValues(string[] values, TreeNodeEx node) { if (node == null) { return; } if (values.Contains(node.Value)) { node.Checked = true; } else { if (node.Nodes != null && node.Nodes.Count > 0) { foreach (TreeNodeEx item in node.Nodes) { this.SetCheckedNodeByValues(values, item); } } } }
private List <TreeNodeEx> GetCheckedNodes(TreeNodeEx node) { if (node != null && this.PopDownTree.CheckBoxes) { List <TreeNodeEx> list = new List <TreeNodeEx>(); if (node.Checked) { list.Add(node); } if (node.Nodes != null && node.Nodes.Count > 0) { list.AddRange(this.GetCheckedNodes(node)); } return(list); } else { return(null); } }
/// <summary> /// BindChildNode 绑定子节点(指定数据源) /// </summary> /// <param name="node">The node.</param> /// <param name="dataSource">The data source.</param> /// User:Ryan CreateTime:2012-9-27 17:40. protected virtual void BindChildNode(TreeNodeEx node, object dataSource) { //验证绑定树的深度:如果TreeMaxDegree为0标示无限级数,其它只绑定相应级数的数据 if (node != null && dataSource != null && (this.TreeMaxDegree == 0 || node.Level < this.TreeMaxDegree - 1)) { node.Datasource = dataSource; //get data source IEnumerable source = this.GetSource(dataSource); if (source == null) { throw new ArgumentException("数据源必须是支持枚举器的列表数据!"); } //bind data node.Nodes.Clear(); foreach (object item in source) { TreeNodeEx childNode = this.GetChildNode(node, item); node.Nodes.Add(childNode); this.BindChildNode(item, childNode); } } }
/// <summary> /// 更新文本框显示的文本 /// </summary> /// <param name="node">The node.</param> /// User:Ryan CreateTime:2012-9-27 21:58. private void UpdateText(TreeNodeEx node) { if (node == null) { return; } if (this.CheckBox) { this.Text = this.GetCheckedTexts(); } else { if (this.ShowFullPathText) { this.Text = node.FullPath; } else { this.Text = node.Text; } } }
private void SetSelectedNodeByValue(string value, TreeNodeEx node) { if (node == null) { return; } if (node.Value.Equals(value)) { //this.SelectedNode = node; this.SetSelectedNode(node); return; } else { if (node.Nodes != null && node.Nodes.Count > 0) { foreach (TreeNodeEx item in node.Nodes) { this.SetSelectedNodeByValue(value, item); } } } }
protected virtual TreeNodeEx GetChildNode(TreeNodeEx node, object item) { return(new TreeNodeEx(this, item, node.Level == this.TreeMaxDegree - 2 ? true : false)); }