문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top