How to prevent selection of TreeViewItem based on condition

I have a wpf TreeView - bound to some data. The treeview is located to the left of the window, divided into two areas, where the tree is navigation, and the panel on the right side changes the content depending on the selected node tree.

Not all nodes of the tree see detailed information. I want to disable the selection of these nodes. Any idea?

thanks

+8
wpf xaml treeview treeviewitem
source share
2 answers

@ jama64: you can achieve what you want if you change the style from the IsEnabled property to Focusable.

<TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="Focusable" Value="{Binding HasDetails}"/> </Style> </TreeView.ItemContainerStyle> 
+18
source share

Do you have something like a boolean property in your source called HasDetails or something else? In this case, you can use something like this. Create a MultiDataTrigger in the ItemContainerStyle that binds to HasDetails in the DataContext and IsSelected for the TreeViewItem , and if both are True (well, True, that HasDetails False :-), you run a storyboard that "cancels" the newly selected TreeViewItem .

This will turn off the selection for all TreeViewItem that have no details, but they will still be extensible. Hope this was what you were looking for

 <TreeView ...> <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding HasDetails}" Value="False"/> <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True"/> </MultiDataTrigger.Conditions> <MultiDataTrigger.EnterActions> <BeginStoryboard> <Storyboard> <BooleanAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(TreeViewItem.IsSelected)"> <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False"/> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </MultiDataTrigger.EnterActions> </MultiDataTrigger> </Style.Triggers> </Style> </TreeView.ItemContainerStyle> </TreeView> 

Refresh

To disable TreeViewItem where HasDetails is False, you can use this

 <TreeView ...> <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsEnabled" Value="{Binding HasDetails}"/> </Style> </TreeView.ItemContainerStyle> </TreeView> 
+6
source share

All Articles