My TreeView nodes have a checkbox. If the User checks one checkbox, all other checkboxes from the rootnode subnodecollection should automatically be disabled.
Here is my treeview:
<TreeView ItemsSource="{Binding Items}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" >
<Style.Triggers>
<Trigger Property="HasItems" Value="true">
<Setter Property="Focusable" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding SubNodes}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0"/>
<TextBlock Text="{Binding Text}" Grid.Column="1"/>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Modifying the ViewModel is the way to go:
Each item in the SubNodes collection should implement a new bool property, ‘Enabled’ for example and another bool ‘Checked’.
Then bind ‘Checked’ to the ‘IsChecked’ property of the Checkbox and ‘Enabled’ to ‘IsEnabled’ and build the necessary logic in the set accesor of ‘Checked’ to set to false the ‘Enabled’ property in every other item of the collection.