【问题标题】:c# tree view, select the child node when select he parent nodec#树视图,选择父节点时选择子节点
【发布时间】:2014-12-08 15:48:08
【问题描述】:

我创建一个树视图。将此树视图与数据库绑定。

我希望如果我选择父节点,所有子节点都应该自动选择。

在 c# 中我能做什么??

【问题讨论】:

    标签: c# tree


    【解决方案1】:

    这是你想要的:

    // Updates all child tree nodes recursively.
    private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
    {
       foreach(TreeNode node in treeNode.Nodes)
       {
          node.Checked = nodeChecked;
          if(node.Nodes.Count > 0)
          {
             // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
             this.CheckAllChildNodes(node, nodeChecked);
          }
       }
    }
    
    // NOTE   This code can be added to the BeforeCheck event handler instead of the AfterCheck event.
    // After a tree node's Checked property is changed, all its child nodes are updated to the same value.
    private void node_AfterCheck(object sender, TreeViewEventArgs e)
    {
       // The code only executes if the user caused the checked state to change.
       if(e.Action != TreeViewAction.Unknown)
       {
          if(e.Node.Nodes.Count > 0)
          {
             /* Calls the CheckAllChildNodes method, passing in the current 
             Checked value of the TreeNode whose checked state changed. */
             this.CheckAllChildNodes(e.Node, e.Node.Checked);
          }
       }
    }
    

    【讨论】:

      【解决方案2】:

      当您说“选中”时,您是指复选框吗?如果您仅指“突出显示”,那么这与树控件的设计背道而驰 - 任何时候只能突出显示一个分支/叶子(以显示您当前选择的那个)。如果您的意思是勾选父分支的复选框同时勾选所有子分支的复选框,那么您需要响应单击分支时触发的事件,检查选中状态,并手动将分支的子项带到设置他们的检查状态。

      【讨论】:

        【解决方案3】:

        上面的代码不能可靠地工作 - 它是来自 MSDN AfterCheck 事件主题的复制/粘贴代码,但是该事件在双击时不会可靠地触发 - 您需要混合禁用双击的代码 - 这就是我发现的作为 MSDN 中的解决方法:

        public class MyTreeView : TreeView
        {
            #region Constructors
        
            public MyTreeView()
            {
            }
        
            #endregion
        
            #region Overrides
        
            protected override void WndProc(ref Message m)
            {
                // Suppress WM_LBUTTONDBLCLK on checkbox
                if (m.Msg == 0x0203 && CheckBoxes && IsOnCheckBox(m))
                {
                    m.Result = IntPtr.Zero;
                }
                else
                {
                    base.WndProc(ref m);
                }
            }
        
            #endregion
        
            #region Double-click check
        
            private int GetXLParam(IntPtr lParam)
            {
                return lParam.ToInt32() & 0xffff;
            }
        
            private int GetYLParam(IntPtr lParam)
            {
                return lParam.ToInt32() >> 16;
            }
        
            private bool IsOnCheckBox(Message m)
            {
                int x = GetXLParam(m.LParam);
                int y = GetYLParam(m.LParam);
                TreeNode node = GetNodeAt(x, y);
                if (node == null)
                    return false;
                int iconWidth = ImageList == null ? 0 : ImageList.ImageSize.Width;
                int right = node.Bounds.Left - 4 - iconWidth;
                int left = right - CHECKBOX_WIDTH;
                return left <= x && x <= right;
            }
        
            const int CHECKBOX_WIDTH = 12;
        
            #endregion
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多