How to create unselected TreeViewItem in WPF

I bind TreeView. My reason is tree level 0 is not selectable. When I click on level 0 of the treeviewitem, the current element must be compensated, and the first child element must be selected.

├ Item 1 //<- level 0. this item must be unselectable ├─ Child Item 11 //<- level 1 ├─ Child Item 12 ├ Item 2 //<- level 0. When i click this item, that is automatically collapse ├─ Child Item 21 ├─ Child Item 22 

How to do this with style?

+4
source share
1 answer

I would do it in my model. The presentation model for level 0 elements will have:

 public bool IsSelected { get { return false; } set { // error checking is omitted Children[0].IsSelected = value; // let WPF know that IsSelected may have changed from what it expecting this.Dispatcher.BeginInvoke((ThreadStart)delegate { this.OnPropertyChanged(() => this.IsSelected); }); } } 

Your XAML will look like this:

 <TreeView> <TreeView.ItemContainerStyle> <Style TargetType="TreeViewItem"> <Setter Property="IsSelected" Value="{Binding IsSelected}"/> </Style> </TreeView.ItemContainerStyle> </TreeView> 

Now, when the user clicks an element of level one, the virtual machine refuses to choose and instead selects its first child element.

You can use exactly the same technique to cope with your requirements for rolling up levels.

+7
source

Source: https://habr.com/ru/post/1308548/


All Articles