I have a TreeView that is populated with various types of elements. Elements can be of type Node(then they can have children) or type Entry(then they have no children). To do this, I bound my TreeView to the ViewModel property AllNodesAndEntries, which is ObservableCollection<object>. For different types Node, EntryI also defined two DataTemplates. Here is the code:
<TreeView ItemsSource="{Binding AllNodesAndEntries}">
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Children}"
DataType="{x:Type local:Node}">
<TextBlock Text="{Binding Name}"
Background="LightBlue"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:Entry}">
<TextBlock Text="{Binding Name}"
Background="LightSalmon"/>
</DataTemplate>
</TreeView.Resources>
</TreeView>
Now I want to make the elements Entryunmanageable if a certain condition is met (that is, if my ViewModel property MyPropis equal true).
So, I added a trigger to the DataTemplate for Entryas follows:
<DataTemplate DataType="{x:Type local:Entry}">
<TextBlock Text="{Binding Name}"
Background="LightSalmon"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding MyProp}" Value="True">
<Setter Property="Focusable" Value="False"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
, , MyProp true. ? ?
a NotifyPropertyChanged(nameof(MyProp)); MyProp, MyProp .