简体   繁体   English

如何使TreeViewItem不可折叠

[英]How to make TreeViewItem non collapsible

I am working on a WPF project and I have a TreeView using HierarchicalDataTemplates . 我正在开发一个WPF项目,我有一个使用HierarchicalDataTemplatesTreeView 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. TreeViewItem默认模板定义了一个ToggleButton ,它显示和隐藏子项。 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 . 您可以根据IsEnabled属性绑定到项目上某个属性的模板创建自己的模板,这样就可以防止它被切换,当然您也应该绑定IsExpanded (也可以在模板外部完成) )并且该案例中的值应始终为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM