Question

I am working on a WPF project and I have a TreeView using HierarchicalDataTemplates. I have been able to establish some different levels of nodes. Everything is going well so far.

<TreeView Margin="14,14,14,14" Name="treeView" ItemsSource="{Binding Tree}"
                               BorderThickness="0">
                  <TreeView.Resources>
                    <!--
                        First Level
                    -->
                    <HierarchicalDataTemplate DataType="{x:Type vm:FirstLevelViewModel}" 
                                              ItemsSource="{Binding Children}" >
                        <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="{Binding SomeText}" FontSize="14" FontWeight="Bold" Foreground="DarkBlue"  />
                        </StackPanel>                           
                    </HierarchicalDataTemplate>
                    <!--
                        Second Level
                    -->
                    <HierarchicalDataTemplate DataType="{x:Type vm:SecondLevelViewModel}" 
                                              ItemsSource="{Binding Children}" >
                            <CheckBox Name="checkBox" IsChecked="{Binding IsChecked}" IsEnabled="{Binding IsEnabled}">
                            <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding SomeText}" FontSize="14" />
                            </StackPanel>
                        </CheckBox>
                    </HierarchicalDataTemplate>
. . . . 

My problem is that: I need some nodes to be non collapsible.

Is there any way to achieve that? I have been searching about it with no luck.

Was it helpful?

Solution

The default template of the TreeViewItem defines a ToggleButton which shows and hides the sub-items. You can create your own template based on that in which the IsEnabled property is bound to some property on your item, so that it can be prevented from being toggled, of course you should also bind the IsExpanded (can be done outside the template as well) and the value in that case should always be true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top