.NET Framework Bookmark and Share   
 index > Windows Presentation Foundation (WPF) > how do you refere to a treeviewitem in a template
 

how do you refere to a treeviewitem in a template

Hello

I want to add this piece of code to a template.
  <EventTrigger RoutedEvent="Mouse.PreviewMouseDown" SourceName="Grid1" >
      <Setter  Property="IsSelected"
                Value="true"/>
   </EventTrigger> 
I want to have the treeviewitem selected when the mouse goes down with the above code but in the setter i don't know how to refer to the treeviewitem itself

marck68
With the code provide, the item gets selected on mouse down event. There might be something missing because i tested it without TreeViewItemFocusVisual and ExpandCollapseToggleStyle Styles that where not provided.

Regarding the piece of code from your first post:
<EventTrigger RoutedEvent="Mouse.PreviewMouseDown" SourceName="Grid1" >
  <Setter  Property="IsSelected"
        Value="true"/>
</EventTrigger>
-Supposing that the UIElement named Grid1 is a Grid (not present in the Style posted) the RoutedEvent should be, since raised in a Grid UIElement, "Grid.PreviewMouseDown".

-EventTriggers do hot have Setters but TriggerActions like BeginStoryboard. So if you want to set bool typed property in a RoutedEvent you have to use a BooleanAnimationUsingKeyFrames. Something like so:
<EventTrigger RoutedEvent="Grid.PreviewMouseDown">
  <BeginStoryboard>
    <Storyboard >
      <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
        <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
      </BooleanAnimationUsingKeyFrames>
    </Storyboard>
  </BeginStoryboard>
</EventTrigger>
This is far from a good implementation but this might be what you're looking for:
<Style x:Key="{x:Type TreeViewItem}"
TargetType="{x:Type TreeViewItem}">
    <Setter Property="Background"
Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment"
Value="{Binding Path=HorizontalContentAlignment,
      RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment"
Value="{Binding Path=VerticalContentAlignment,
      RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Padding"
Value="1,0,0,0"/>
    <Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <!--<Setter Property="FocusVisualStyle"
Value="{StaticResource TreeViewItemFocusVisual}"/>-->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                <Grid x:Name="Grid1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="19"
              Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <ToggleButton x:Name="Expander"
          
          IsChecked="{Binding Path=IsExpanded,
                      RelativeSource={RelativeSource TemplatedParent}}"
          ClickMode="Press"/>
                    <Border Name="Bd"
      Grid.Column="1"
      Background="{TemplateBinding Background}"
      BorderBrush="{TemplateBinding BorderBrush}"
      BorderThickness="{TemplateBinding BorderThickness}"
      Padding="{TemplateBinding Padding}">
                        <ContentPresenter x:Name="PART_Header"
              ContentSource="Header"
              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                    </Border>
                    <ItemsPresenter x:Name="ItemsHost"
          Grid.Row="1"
          Grid.Column="1"
          Grid.ColumnSpan="2"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded"
       Value="false">
                        <Setter TargetName="ItemsHost"
        Property="Visibility"
        Value="Collapsed"/>
                    </Trigger>
                    <Trigger Property="HasItems"
       Value="false">
                        <Setter TargetName="Expander"
        Property="Visibility"
        Value="Hidden"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader"
             Value="false"/>
                            <Condition Property="Width"
             Value="Auto"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header"
        Property="MinWidth"
        Value="75"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader"
             Value="false"/>
                            <Condition Property="Height"
             Value="Auto"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header"
        Property="MinHeight"
        Value="19"/>
                    </MultiTrigger>
                    <Trigger Property="IsSelected"
       Value="true">
                        <Setter TargetName="Bd"
        Property="Background"
        Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        <Setter Property="Foreground"
        Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected"
             Value="true"/>
                            <Condition Property="IsSelectionActive"
             Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="Bd"
        Property="Background"
        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        <Setter Property="Foreground"
        Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled"
       Value="false">
                        <Setter Property="Foreground"
        Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <EventTrigger RoutedEvent="Grid.MouseEnter">
            <BeginStoryboard>
                <Storyboard >
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
                        <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Grid.MouseLeave">
            <BeginStoryboard>
                <Storyboard >
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
                        <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0"/>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>
Set event on MouseEnter to make it more visible. If you set focus on the TreeView control, the effect becomes even more clear.


Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
  • Marked As Answer bymarck68 4 hours 5 minutes ago
  •  
Bigsby
If you name the TreeViewItem control you can set Setter TargetName Property.
Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
Bigsby
where do I have to put the name in the following template?


<Style x:Key="{x:Type TreeViewItem}"
     TargetType="{x:Type TreeViewItem}">
  <Setter Property="Background"
      Value="Transparent"/>
  <Setter Property="HorizontalContentAlignment"
      Value="{Binding Path=HorizontalContentAlignment,
              RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  <Setter Property="VerticalContentAlignment"
      Value="{Binding Path=VerticalContentAlignment,
              RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  <Setter Property="Padding"
      Value="1,0,0,0"/>
  <Setter Property="Foreground"
      Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  <Setter Property="FocusVisualStyle"
      Value="{StaticResource TreeViewItemFocusVisual}"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type TreeViewItem}">
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="19"
                      Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
          </Grid.RowDefinitions>
          <ToggleButton x:Name="Expander"
                  Style="{StaticResource ExpandCollapseToggleStyle}"
                  IsChecked="{Binding Path=IsExpanded,
                              RelativeSource={RelativeSource TemplatedParent}}"
                  ClickMode="Press"/>
          <Border Name="Bd"
              Grid.Column="1"
              Background="{TemplateBinding Background}"
              BorderBrush="{TemplateBinding BorderBrush}"
              BorderThickness="{TemplateBinding BorderThickness}"
              Padding="{TemplateBinding Padding}">
            <ContentPresenter x:Name="PART_Header"
                      ContentSource="Header"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
          </Border>
          <ItemsPresenter x:Name="ItemsHost"
                  Grid.Row="1"
                  Grid.Column="1"
                  Grid.ColumnSpan="2"/>
        </Grid>
        <ControlTemplate.Triggers>
          <Trigger Property="IsExpanded"
               Value="false">
            <Setter TargetName="ItemsHost"
                Property="Visibility"
                Value="Collapsed"/>
          </Trigger>
          <Trigger Property="HasItems"
               Value="false">
            <Setter TargetName="Expander"
                Property="Visibility"
                Value="Hidden"/>
          </Trigger>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="HasHeader"
                     Value="false"/>
              <Condition Property="Width"
                     Value="Auto"/>
            </MultiTrigger.Conditions>
            <Setter TargetName="PART_Header"
                Property="MinWidth"
                Value="75"/>
          </MultiTrigger>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="HasHeader"
                     Value="false"/>
              <Condition Property="Height"
                     Value="Auto"/>
            </MultiTrigger.Conditions>
            <Setter TargetName="PART_Header"
                Property="MinHeight"
                Value="19"/>
          </MultiTrigger>
          <Trigger Property="IsSelected"
               Value="true">
            <Setter TargetName="Bd"
                Property="Background"
                Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
            <Setter Property="Foreground"
                Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
          </Trigger>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="IsSelected"
                     Value="true"/>
              <Condition Property="IsSelectionActive"
                     Value="false"/>
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                Property="Background"
                Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
            <Setter Property="Foreground"
                Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
          </MultiTrigger>
          <Trigger Property="IsEnabled"
               Value="false">
            <Setter Property="Foreground"
                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>



marck68
With the code provide, the item gets selected on mouse down event. There might be something missing because i tested it without TreeViewItemFocusVisual and ExpandCollapseToggleStyle Styles that where not provided.

Regarding the piece of code from your first post:
<EventTrigger RoutedEvent="Mouse.PreviewMouseDown" SourceName="Grid1" >
  <Setter  Property="IsSelected"
        Value="true"/>
</EventTrigger>
-Supposing that the UIElement named Grid1 is a Grid (not present in the Style posted) the RoutedEvent should be, since raised in a Grid UIElement, "Grid.PreviewMouseDown".

-EventTriggers do hot have Setters but TriggerActions like BeginStoryboard. So if you want to set bool typed property in a RoutedEvent you have to use a BooleanAnimationUsingKeyFrames. Something like so:
<EventTrigger RoutedEvent="Grid.PreviewMouseDown">
  <BeginStoryboard>
    <Storyboard >
      <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
        <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
      </BooleanAnimationUsingKeyFrames>
    </Storyboard>
  </BeginStoryboard>
</EventTrigger>
This is far from a good implementation but this might be what you're looking for:
<Style x:Key="{x:Type TreeViewItem}"
TargetType="{x:Type TreeViewItem}">
    <Setter Property="Background"
Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment"
Value="{Binding Path=HorizontalContentAlignment,
      RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment"
Value="{Binding Path=VerticalContentAlignment,
      RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Padding"
Value="1,0,0,0"/>
    <Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <!--<Setter Property="FocusVisualStyle"
Value="{StaticResource TreeViewItemFocusVisual}"/>-->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                <Grid x:Name="Grid1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="19"
              Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <ToggleButton x:Name="Expander"
          
          IsChecked="{Binding Path=IsExpanded,
                      RelativeSource={RelativeSource TemplatedParent}}"
          ClickMode="Press"/>
                    <Border Name="Bd"
      Grid.Column="1"
      Background="{TemplateBinding Background}"
      BorderBrush="{TemplateBinding BorderBrush}"
      BorderThickness="{TemplateBinding BorderThickness}"
      Padding="{TemplateBinding Padding}">
                        <ContentPresenter x:Name="PART_Header"
              ContentSource="Header"
              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                    </Border>
                    <ItemsPresenter x:Name="ItemsHost"
          Grid.Row="1"
          Grid.Column="1"
          Grid.ColumnSpan="2"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsExpanded"
       Value="false">
                        <Setter TargetName="ItemsHost"
        Property="Visibility"
        Value="Collapsed"/>
                    </Trigger>
                    <Trigger Property="HasItems"
       Value="false">
                        <Setter TargetName="Expander"
        Property="Visibility"
        Value="Hidden"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader"
             Value="false"/>
                            <Condition Property="Width"
             Value="Auto"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header"
        Property="MinWidth"
        Value="75"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasHeader"
             Value="false"/>
                            <Condition Property="Height"
             Value="Auto"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="PART_Header"
        Property="MinHeight"
        Value="19"/>
                    </MultiTrigger>
                    <Trigger Property="IsSelected"
       Value="true">
                        <Setter TargetName="Bd"
        Property="Background"
        Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        <Setter Property="Foreground"
        Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected"
             Value="true"/>
                            <Condition Property="IsSelectionActive"
             Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter TargetName="Bd"
        Property="Background"
        Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        <Setter Property="Foreground"
        Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled"
       Value="false">
                        <Setter Property="Foreground"
        Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <EventTrigger RoutedEvent="Grid.MouseEnter">
            <BeginStoryboard>
                <Storyboard >
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
                        <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <EventTrigger RoutedEvent="Grid.MouseLeave">
            <BeginStoryboard>
                <Storyboard >
                    <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsSelected">
                        <DiscreteBooleanKeyFrame Value="False" KeyTime="0:0:0"/>
                    </BooleanAnimationUsingKeyFrames>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Style.Triggers>
</Style>
Set event on MouseEnter to make it more visible. If you set focus on the TreeView control, the effect becomes even more clear.


Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
  • Marked As Answer bymarck68 4 hours 5 minutes ago
  •  
Bigsby

You can use google to search for other answers

Custom Search

More Threads

• Why Image is overlayed with reddish layer in WPF?
• CAD with WPF: "Capture" External TabletDevice
• How to hide the Tab Headers for a TabControl?
• WPF store layout in resources
• Rotate transform
• How to trap event in CheckBox control?
• the calling thread cannot access?
• BitmapEffect: Change programmaticaly
• Issues binding to a property of an object that belongs to another object
• GeometryResult.IntersectionDetail=InterSectionDetail.Intersect is TRUE Sometimes when there is clearly no intersection. Please Help!