简体   繁体   English

WPF Treeview项目选择问题

[英]WPF Treeview Item select Problem

替代文字

I Have Treeview (shown as above) in my app, I have binded it with collection... now the problem is whenever I expand on Colorodo by default Aspen should get selected, means whenever I expand first item that Node should get selected.. 我的应用程序中有Treeview(如上图所示),我已将其与收藏绑定...现在的问题是,默认情况下,每当我在Colorodo上进行扩展时,就应该选择Aspen ,这意味着每当我扩展第一个项时,就应该选择Node。

Any Ideas/suggestion Please 有任何想法/建议

Unfortunately, as I'm sure you have discovered, is that you cannot set the treeViewInstance.SelectedItem property as it's read-only. 不幸的是,正如我确定您已经发现的那样,您无法将treeViewInstance.SelectedItem属性设置为只读。

From memory, each TreeViewItem has a IsSelected property that you can set. 从内存中,每个TreeViewItem可以设置一个IsSelected属性。 You try to listen for expand/collapse on the items and maybe set this property. 您尝试侦听项目的展开/折叠,并可能设置此属性。 Without trying this myself I don't know if it is a) a good solution b) if it would even works. 如果不自己尝试,我不知道它是否是a)一个好的解决方案b)它是否会起作用。

To get the TreeViewItem that is the container for the item in collection use 要获取TreeViewItem ,它是集合中项目的容器,请使用

treeViewInstance.ItemContainerGenerator.ContainerFromItem(yourDataItem) as TreeViewItem;

Another idea (the way I would do it) is to use a ListBox/ListView and fake the hierarchical view. 另一个想法(我这样做的方式)是使用ListBox / ListView并伪造分层视图。 The create a view model controller and item, wrap your data, and manage this all yourself. 创建一个视图模型控制器和项目,包装您的数据,并亲自管理所有这些。 If you want more information, please leave a comment and I will dig up a few examples to help. 如果您需要更多信息,请发表评论,我将列举一些示例来提供帮助。

HTH, HTH,

Dennis 丹尼斯

There are two apporches choose what ever you like. 您可以选择两种方式选择自己喜欢的东西。 i) explore ItemTemplageSelector, not sure but may be possible to do work with it. i)不确定ItemTemplageSelector,但可能可以使用它。

Other wise on tree expand event or mouse capture event get the Current Root node and then select its first child with index like rootNode.child[0]... set this is as Selected True or Isfocused and perfom operation that is intended on its click you you will make user feel like it is selected. 否则,在树展开事件或鼠标捕获事件上,获取“当前根”节点,然后选择其第一个索引为rootNode.child [0]的子节点。将其设置为“选定的True”或“ Isfocused”,并执行其单击操作您将使用户感到被选中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM