简体   繁体   中英

How to change the way the selection is handled in a WPF TreeView

when I remove an item that is currently selected from the TreeView automatically the parent gets selected. I would like to change this behavior so the previous or the next child gets selected. I really don't know where to start ...

Any idea on how to accomplish this would be great!

Thanks.

You can set the SelectedItem by introducing a property such as IsSelected in your tree view node's datacontext class or model.

Assuming that you are binding a hierarchy of TreeViewItemModel class to the TreeView, you need to do the following

  1. Add writeable IsSelected propertyb in TreeViewItemModel . Remember to raise property changed notification in the Setter of IsSelected .

  2. Introduce this in the TreeView resources ...

     <TreeView.Resources> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> </Style> </TreeView.Resources> 
  3. After you delete a node, set the next or previous tree view child's TreeViewItemModel objects IsSelected as true.

Let me know if this helps.

TreeView has Items property, which is of type ItemCollection . This type has some good events, like CollectionChanged or CurrentChanged. may be you should to spade this way?

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