C# 停止选择一个或多个 TreeNode 的 Treeview
我有一个 TreeView 控件,显示有组织的层次结构中的多个 TreeNode。 我想阻止用户选择最高级别的节点(这是通过使用 BeforeSelect 事件实现的)。 如果用户选择顶级节点,我还想阻止 TreeView 突出显示顶级节点,即阻止 TreeView 更改节点的背景颜色并“选择”它。
我使用的 TreeView 是 WinForms 版本的控件。
下面是我当前尝试使用的源代码:
private void tree_BeforeSelect ( object sender, TreeViewCancelEventArgs e )
{
if ( e.Node.Level == 0 )
{
e.Cancel = true;
}
}
这确实会取消选择节点,但只有在明显的闪烁(~200ms)之后才会取消选择,这是不希望的。
I have a TreeView control showing multiple TreeNodes in an organised heirarchy. I want to stop the user selecting the highest level Nodes (this was achieved by using the BeforeSelect Event). I also want to stop the TreeView from highlighting the top level nodes if the user selects them i.e. stop the TreeView from changing the background color of the node and 'selecting' it.
The TreeView that I am using is the WinForms version of the control.
Below is the source code I am currently attempting to use:
private void tree_BeforeSelect ( object sender, TreeViewCancelEventArgs e )
{
if ( e.Node.Level == 0 )
{
e.Cancel = true;
}
}
This does de-select the Node but only after a noticible flash (~200ms) which is undesirable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此代码可防止在取消选择之前绘制选择:
This code prevents drawing the selection before it is canceled:
除了现有代码之外,如果您使用代码向 TreeView 上的 MouseDown 事件添加处理程序并使用其位置选择节点,则可以设置节点颜色。
仍然存在一个小问题,即选择轮廓仍然显示在 MouseDown 上,但它至少会停止蓝色背景并让您走得更远。
HTH
OneSHOOT
In addition to your existing code if you add a handler to the MouseDown event on the TreeView with the code and select the node out using it's location, you can then set the nodes colours.
There is still a slight problem in that the select outline still shows on MouseDown but it atleast stops the blue background and gets you a little further.
HTH
OneSHOT
如果通过在 BeforeSelect 的事件参数中将 Cancel 设置为 true 来取消选择,则不会选择该节点,因此背景颜色不会更改。
If the selecting is cancelled by setting Cancel to true in the BeforeSelect's event args, the node will not be selected and thus the background color won't change.