locked
Non Collapsible TreeViewItem RRS feed

  • 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.


    Monday, August 27, 2012 8:45 PM

Answers

  • For the TreeView, you need to set up the ItemContainerStyle and then use a style something like

     <Style x:Key="TreeViewStyle" TargetType="{x:Type TreeViewItem}">
         <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
     </Style>
    

    You can set the IsExpanded based on different criteria.  Another option would be using a Converter to set the IsExpanded property and putting your criteria in there instead. 

    Hope that helps!


    Christine A. Piffat

    • Proposed as answer by agrawal.ashish Tuesday, August 28, 2012 4:10 AM
    • Marked as answer by Min Zhu Tuesday, September 11, 2012 9:53 AM
    Tuesday, August 28, 2012 3:17 AM
  • Hi

    you need to set like this for treeviewitem to be expanded and toggled button to be noncollapsible

     <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="True" />
                    <Setter Property="IsHitTestVisible" Value="False" />
                </Style>

    so you need to set the binding for these from your property like this

    <Style  TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="{Binding ToBeExpanded}" />
                    <Setter Property="IsHitTestVisible" Value="{Binding TobeToggled}" />
                </Style>

    Tuesday, August 28, 2012 4:24 AM

All replies

  • For the TreeView, you need to set up the ItemContainerStyle and then use a style something like

     <Style x:Key="TreeViewStyle" TargetType="{x:Type TreeViewItem}">
         <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
     </Style>
    

    You can set the IsExpanded based on different criteria.  Another option would be using a Converter to set the IsExpanded property and putting your criteria in there instead. 

    Hope that helps!


    Christine A. Piffat

    • Proposed as answer by agrawal.ashish Tuesday, August 28, 2012 4:10 AM
    • Marked as answer by Min Zhu Tuesday, September 11, 2012 9:53 AM
    Tuesday, August 28, 2012 3:17 AM
  • Hi

    you need to set like this for treeviewitem to be expanded and toggled button to be noncollapsible

     <Style TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="True" />
                    <Setter Property="IsHitTestVisible" Value="False" />
                </Style>

    so you need to set the binding for these from your property like this

    <Style  TargetType="TreeViewItem">
                    <Setter Property="IsExpanded" Value="{Binding ToBeExpanded}" />
                    <Setter Property="IsHitTestVisible" Value="{Binding TobeToggled}" />
                </Style>

    Tuesday, August 28, 2012 4:24 AM