TreeViewItem isSelected 触发两次
我有一个包含各种项目的 TreeView。项目的样式设置为将 IsSelected 属性绑定到我的 VM 的 bool IsSelected。
每当我单击树视图项时,该 VM 属性都会被调用两次,第一次是 value==false,然后是 value==true。
我认为这是正常行为,但我不确定为什么,因为我以相同的方式绑定了 IsExpished 属性,并且仅被调用一次。
谢谢
I have a TreeView with various items. the Items are styled such that the IsSelected property is bound to my VM to a bool IsSelected.
whenever I click a tree view item, this VM property is called twice, first with value==false then with value==true.
I assume this is a normal behavior, but I am not sure why, given that I have the IsExpended property bound in the same way, and is only called once.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑原因是当您选择一个项目时,首先取消选择旧项目,然后选择新项目。这会导致第一次调用为 false,第二次调用为 true。
也许在调试时,检查视图模型上的 DisplayName 或类似属性,以查看每次修改哪些项目,看看是否确实如此。
同样,当您展开一个节点时,不需要折叠任何其他节点。因此,它只是展开您尝试展开的节点(为 IsExpanded 传递 true)。
I suspect the reason is that when you select an item, first the old item is deselected, then the new item is selected. This results in a first call of false, and a second of true.
Perhaps while debugging, check a DisplayName or similar property on your view model, to see which items are being modified each time, to see if this is indeed the case.
Likewise when you expand a node, there's no need for any other nodes to be collapsed. Therefore, it simply expands the node you tried to expand (passing true for IsExpanded).
其原因是冒泡事件。
当一个事件被引发时,它会“冒泡”直到它得到处理。
您可以通过选择第三级项目来验证它。
您只需要设置
e.Handled = true
;奥菲尔
The reason for this is bubbling events.
When an event is raised, it will "bubble" up until it handled.
you can validate it by selecting a third level item.
You just need to set
e.Handled = true
;Ofir