使用复选框在Treeview上操作 [英] Operation on Treeview with checkbox

查看:74
本文介绍了使用复选框在Treeview上操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

非常厌倦找到&实施解决方案,比如,

如果我检查了

PARENT - 检查所有Childs&

如果未选中任何一个CHILD,它的孩子&它的父母应该是不受限制的看,

[√]父母

| - [√]孩子1

| - [√]孩子1.1

| - [√]儿童1.2

| - [√]儿童2

| - [√]儿童2.1

| - [√]儿童2.2





然后



[]父母

| - [√]儿童1

| - [√]儿童1.1

| - [√]儿童1.2

| - [] Child2

| - []儿童2.1

| - []儿童2.2



取消选中Child2

PARENT应该是未选中的。

Hello,
very tired with finding & implementing solution like,
If I checked
PARENT -Check all the Childs &
if Unchecked any single CHILD, child of it & PARENT of it should be unchecked see,
[√] Parent
|-[√]Child 1
|-[√]Child 1.1
|-[√]Child 1.2
|-[√]Child2
|-[√]Child 2.1
|-[√]Child 2.2


And Then

[ ] Parent
|-[√]Child 1
|-[√]Child 1.1
|-[√]Child 1.2
|-[ ]Child2
|-[ ]Child 2.1
|-[ ]Child 2.2

On unchecking on Child2
PARENT should be unchecked.

推荐答案

请参阅我们对该问题的评论。很可能,你的意思是 System.Windows.Forms.TreeView



如果是这样,你可以捕获事件通过处理以下两个事件之一来更改父节点的已检查状态:

TreeView.AfterCheck事件(System.Windows.Forms) [ ^ ],

< a href =https://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.beforecheck(v=vs.110).aspx> TreeView.BeforeCheck事件(System.Windows.Forms) ) [ ^ ]。



使用属性 System.Windows执行其他节点的检查.Forms.TreeNode.Checked

TreeNode.Checked属性(System.Windows.Forms) ) [ ^ ]。



您将需要即将更改或更改其检查状态的节点。对此节点的引用将通过事件参数参数传递给您的事件处理程序:

TreeViewEventArgs类(System.Windows.Forms) [ ^ ],

TreeViewEventArgs.Node属性(System.Windows.Forms) [ ^ ]。



这里你应该格外小心,以避免无限的递归。当您以编程方式更改事件处理程序中的已检查状态时,它最终将调用相同的事件处理程序。因此,您需要对此状态的更改采取不同的操作,这是用户点击的直接结果,也是您的程序化更改的结果。



再次,看看这个上面已经引用的页面: TreeView。 AfterCheck事件(System.Windows.Forms) [ ^ ]。



请参阅备注部分,警告您这个情况。另见代码示例;注意注释代码仅在用户导致检查状态改变时才执行。在您的代码中,您还应该注意这一点。



-SA
Please see our comments to the question. Most likely, you mean System.Windows.Forms.TreeView.

If so, you can capture the event of changing checked state of your parent node by handling one of both of these events:
TreeView.AfterCheck Event (System.Windows.Forms)[^],
TreeView.BeforeCheck Event (System.Windows.Forms)[^].

The check of other nodes is performed using the property System.Windows.Forms.TreeNode.Checked:
TreeNode.Checked Property (System.Windows.Forms)[^].

You will need the node which is about to change or changed its check state. The reference to this node will be passed to your event handler via the event arguments parameter:
TreeViewEventArgs Class (System.Windows.Forms)[^],
TreeViewEventArgs.Node Property (System.Windows.Forms)[^].

You should be extra careful here, to avoid infinite recursion. When you programmatically change the checked state in your event handler, it will eventually call the same event handler. So, you need different action on the change of this state as an immediate result of the user click and as a result of your programmatic change.

Again, look at this page I already referenced above: TreeView.AfterCheck Event (System.Windows.Forms)[^].

See the "Remark" section which warns you about this situation. See also the code sample; pay attention for the comment "The code only executes if the user caused the checked state to change". In your code, you should also take care of that.

—SA


这篇关于使用复选框在Treeview上操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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