Триггеры WPT TreeView DataTemplate

Я разрабатываю приложение WPF с C#.

Я добавил TreeView в окне с DataTemplate. Но мне нужны некоторые триггеры стиля для таких шаблонов данных, как MouseOver, IsFocused и т. Д.

Вы можете помочь мне с этим? Спасибо за помощь.

<TreeView 
   x:Name="twLayer" 
   Background="{x:Null}" 
   HorizontalContentAlignment="Stretch" 
   VerticalContentAlignment="Top" 
   Padding="0" 
   SnapsToDevicePixels="True" 
   ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
   ScrollViewer.VerticalScrollBarVisibility="Auto" 
   BorderThickness="0" 
   Cursor="Hand">
   <TreeView.Resources>
      <HierarchicalDataTemplate DataType="{x:Type loc:LayerItems}">
         <Border Width="250" Height="38" BorderBrush="#FF383838" Background="#FF535353"  BorderThickness="1,0,1,1" Padding="2" OverridesDefaultStyle="True" >
            <StackPanel Orientation="Horizontal" SnapsToDevicePixels="True">
               <Border BorderBrush="Black" BorderThickness="1" SnapsToDevicePixels="True">
                  <Border BorderBrush="White" BorderThickness="1" SnapsToDevicePixels="True">
                     <Image Width="30" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Source="silinecek/layerThumb.png" SnapsToDevicePixels="True" />
                  </Border>
               </Border>
               <TextBlock Text="{Binding Path=Name}" Margin="5,10,0,0" Foreground="White" SnapsToDevicePixels="True"/>
            </StackPanel>
         </Border>
      </HierarchicalDataTemplate>
      <DataTemplate DataType="{x:Type loc:ChannelItem}">
         <Border Width="250" BorderBrush="#FF383838" Background="#FF535353" BorderThickness="1,0,1,1" Padding="2" OverridesDefaultStyle="True" >
            <StackPanel Orientation="Horizontal" SnapsToDevicePixels="True">
               <Border BorderThickness="1" BorderBrush="Black" VerticalAlignment="Center" SnapsToDevicePixels="True">
                  <Border BorderThickness="1" BorderBrush="White" SnapsToDevicePixels="True">
                     <Grid Width="16" Height="16" Background="{Binding Path=ChannelColor}" SnapsToDevicePixels="True"/>
                  </Border>
               </Border>
               <TextBlock Margin="5,3,0,0" Text="{Binding Path=Name}" Foreground="White" FontFamily="Calibri" SnapsToDevicePixels="True"/>
            </StackPanel>
         </Border>
      </DataTemplate>
      <DataTemplate DataType="{x:Type loc:EffectItem}">
         <Border Width="250" Height="18" BorderBrush="#FF383838" Background="#FF535353"  BorderThickness="1,0,1,1" Padding="2"  OverridesDefaultStyle="True">
            <StackPanel Orientation="Horizontal"  SnapsToDevicePixels="True">
               <Image Width="10" Height="10" VerticalAlignment="Center"  HorizontalAlignment="Left" Source="icons/iconFX.png" SnapsToDevicePixels="True"/>
               <TextBlock  Margin="6,0,0,0" Text="{Binding Path=Name}" Foreground="White" FontSize="10" FontFamily="Calibri" SnapsToDevicePixels="True"/>
            </StackPanel>
         </Border>
      </DataTemplate>
   </TreeView.Resources>
</TreeView>

2 ответа

Решение

Вы можете добавить свой Triggerв TriggersCollection из Style как это:

<HierarchicalDataTemplate DataType="{x:Type loc:LayerItems}">
    <Border Width="250" Height="38" BorderBrush="#FF383838" Background="#FF535353"  BorderThickness="1,0,1,1" Padding="2" OverridesDefaultStyle="True" >
        <StackPanel Orientation="Horizontal" SnapsToDevicePixels="True">
            <Border BorderBrush="Black" BorderThickness="1" SnapsToDevicePixels="True">
                <Border BorderBrush="White" BorderThickness="1" SnapsToDevicePixels="True">
                    <Image Width="30" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Source="silinecek/layerThumb.png" SnapsToDevicePixels="True" />
                </Border>
            </Border>
            <TextBlock Text="{Binding Path=Name}" Margin="5,10,0,0" Foreground="White" SnapsToDevicePixels="True"/>
        </StackPanel>
        <Border.Style>
            <Style TargetType="{x:Type Border}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" Value="LightGreen" />
                    </Trigger>
                    <Trigger Property="IsFocused" Value="False">
                        <Setter Property="BorderBrush" Value="Gray" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
    </Border>
</HierarchicalDataTemplate>

Вы можете сделать что-то похожее на корень Border элемент в вашем другом DataTemplate тоже. Вы можете узнать больше из TriggerСтраница класса на MSDN.

Вы можете использовать VisualState как

<VisualStateManager.VisualStateGroups>
   <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                                        Storyboard.TargetName="Border">
                                                <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlMouseOverColor}" />
                                            </ColorAnimationUsingKeyFrames>
                                            <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                                        Storyboard.TargetName="Border">
                                                <EasingColorKeyFrame KeyTime="0" Value="{StaticResource ControlNormalColor}" />
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
 </VisualStateManager.VisualStateGroups>
Другие вопросы по тегам