locked
Treeview and HierarchicalDataTemplate RRS feed

  • Question

  • Hi,

    I have the following HierarchicalDataTemplate, which switches UIElements based on the Binding to my in code Object.InEditMode.

    How do i specify in these templates that the treeviewitem should be expanded?

    <ControlTemplate x:Key="EditableText">
          <TextBox Text="{Binding Path=Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                 BorderThickness="0"
                 BorderBrush="White"
                 Name="Part_EditableText"
                 Loaded="Part_EditableText_Loaded"
                 LostFocus="Part_EditableText_LostFocus"
                 KeyUp="Part_EditableText_KeyUp"/>
        </ControlTemplate>
    
        <ControlTemplate x:Key="ReadOnlyText">
          <TextBlock Text="{Binding Path=Name}"
                ToolTip="{Binding Description}"
                ContextMenu="{StaticResource EditMenu}"
                MouseMove="TextBlock__MouseMove"
                PreviewMouseLeftButtonDown="TextBlock_PreviewMouseLeftButtonDown"
                ContextMenuOpening="EditMenu_ContextMenuOpening">
          </TextBlock>
        </ControlTemplate>
    
        <HierarchicalDataTemplate x:Key="MenuNodeTemplate"
                     ItemsSource="{Binding}">
          <Control x:Name="theControl" 
               Template="{StaticResource ReadOnlyText}"/>
          <HierarchicalDataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=InEditMode,Mode=TwoWay,NotifyOnSourceUpdated=True}" 
                   Value="True">
              <Setter TargetName="theControl" 
                    Property="Template" 
                    Value="{StaticResource EditableText}"/>
            </DataTrigger>
          </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>
    

     

    Tuesday, August 9, 2011 5:22 PM

Answers

  • The DataTemplates will define the look for the data item, not the item container (i.e. the TreeViewItem).  If you want all of the TreeViewItems to be expanded, you can simply customize the ItemContainerStyle on the TreeView like this:

    <TreeView ...>
      <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
          <Setter Property="IsExpanded" Value="True"/>
        </Style>
      </TreeView.ItemContainerStyle>
    </TreeView>
    

     

    • Marked as answer by Min Zhu Monday, August 15, 2011 5:43 AM
    Tuesday, August 9, 2011 5:38 PM