C# TreeView 在数据和显示之间变得不匹配 [英] C# TreeView becomes missmatched between data and display

查看:20
本文介绍了C# TreeView 在数据和显示之间变得不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有复选框的 TreeView

I have a TreeView with checkboxes like

我添加了一些代码来显示选择的内容:

I have added some code to show what is selected:

    /// <summary>
    /// Highlight checked nodes
    /// </summary>
    private void HighlightCheckedNodes()
    {
        foreach (TreeNode topNode in treeView1.Nodes)
        {
            if (topNode.Checked)
            {
                topNode.BackColor = Color.Yellow;
            }
            else
            {
                topNode.BackColor = Color.White;
            }

            foreach (TreeNode myNode in topNode.Nodes)
            {
                // Check whether the tree node is checked.
                if (myNode.Checked)
                {
                    // Set the node's backColor.
                    myNode.BackColor = Color.Yellow;
                }
                else
                {
                    myNode.BackColor = Color.White;
                }
            }
        }
    }

这个函数是从 treeView1_AfterSelect 和 treeView1_AfterCheck 调用的.如果我点击几次,我会看到一些标记为已选中但不是黄色的节点(数据显示未选中),反之亦然.

This function is called from treeView1_AfterSelect and treeView1_AfterCheck. If I click a few times I get some nodes that are marked as checked but not yellow (the data says it's not checked) or vice versa.

那么如何确保数据和显示同步?

So how do I make sure the data and display are in sync?

推荐答案

长话短说,TreeView 有问题

这是一个使用 NativeWindow 而不是从 TreeView 派生的实现.这将允许您在表单上使用现有的 TreeView:

Here's an implementation that uses NativeWindow instead of deriving from TreeView. This will allow you to use the existing TreeView on your Form:

public partial class Form1 : Form
{

    private TreeViewSuppressDoubleClick treeViewHelper;

    private void Form1_Load(object sender, EventArgs e)
    {
        treeViewHelper = new TreeViewSuppressDoubleClick(this.treeView1);
    }

    public class TreeViewSuppressDoubleClick : NativeWindow
    {

        public TreeViewSuppressDoubleClick(TreeView treeView)
        {
            if (treeView != null && treeView.IsHandleCreated)
            {
                this.AssignHandle(treeView.Handle);
            }
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg != 0x203)
                base.WndProc(ref m);
        }

    }
    private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
    {
        e.Node.BackColor = e.Node.Checked ? Color.Yellow : Color.White;
    }

}

此后,您会注意到 TreeView 对点击的响应不那么灵敏,但它现在确实保持同步.

After this you'll notice that the TreeView isn't as responsive to clicks, but it does stay in sync now.

注意在选中/取消选中时更改背景颜色的简化方法.

Note the simplified method for changing the BackColor on a check/uncheck.

此外,这里还有一个使用递归获取所有节点的替代 HighlightCheckedNodes():

Also, here's an alternate HighlightCheckedNodes() using recursion to get all the nodes:

private void HighlightCheckedNodes(TreeNode node)
{
    if (node == null)
    {
        foreach (TreeNode topNode in treeView1.Nodes)
        {
            HighlightCheckedNodes(topNode);
        }
    }
    else
    {
        node.BackColor = node.Checked ? Color.Yellow : Color.White;
        foreach (TreeNode curNode in node.Nodes)
        {
            HighlightCheckedNodes(curNode);
        }
    }
}

您可以使用 HighlightCheckedNodes(null); 调用它.

You'd call it using HighlightCheckedNodes(null);.

这篇关于C# TreeView 在数据和显示之间变得不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆