简体   繁体   中英

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..

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.

From memory, each TreeViewItem has a IsSelected property that you can set. 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.

To get the TreeViewItem that is the container for the item in collection use

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. 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,

Dennis

There are two apporches choose what ever you like. i) explore ItemTemplageSelector, not sure but may be possible to do work with it.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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