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