簡體   English   中英

WPF樣式觸發在TreeView上?

[英]WPF style triggers on a TreeView?

我在xaml中定義了以下樹視圖:

        <TreeView Name="PST_TreeView"
              Grid.Row="0"
              Grid.Column="0"
              Width="Auto"
              Height="Auto"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch"
              ItemsSource="{Binding SitesCollection}"
              ItemTemplate="{StaticResource SitesTemplate}"
              Style="{StaticResource TreeViewStyleBasic}" />

使用針對我的資源文件的資源綁定:

    <Style x:Key="TreeViewStyleBasic" TargetType="TreeView">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="{DynamicResource TitleBarButtons_BorderBrush}" />
    <Setter Property="BorderThickness" Value="0 0 2 0" />
</Style>

<Style x:Key="TreeViewItemStyle_CatNodes" TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground" Value="Snow" />
    <Setter Property="FontFamily" Value="Calibri" />
    <Setter Property="FontSize" Value="16" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="TextAlignment" Value="Left" />
</Style>

<Style x:Key="TreeViewItemStyle_ChildNodes" TargetType="{x:Type TextBlock}">
    <Setter Property="Foreground" Value="Snow" />
    <Setter Property="FontFamily" Value="Calibri" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="FontStyle" Value="Italic" />
    <Setter Property="TextAlignment" Value="Left" />
</Style>

<DataTemplate x:Key="VolumeInfoDataTemplate">
    <StackPanel Orientation="Horizontal">
        <TextBlock Width="{TemplateBinding Width}"
                   Height="{TemplateBinding Height}"
                   Margin="5"
                   Style="{DynamicResource TreeViewItemStyle_ChildNodes}"
                   Text="{Binding VolumeName}" />
    </StackPanel>
</DataTemplate>

<HierarchicalDataTemplate x:Key="SitesTemplate"
                          ItemsSource="{Binding VolumesList}"
                          ItemTemplate="{StaticResource VolumeInfoDataTemplate}">
    <StackPanel Orientation="Horizontal">
        <TextBlock Width="{TemplateBinding Width}"
                   Height="{TemplateBinding Height}"
                   Margin="5"
                   Style="{DynamicResource TreeViewItemStyle_CatNodes}"
                   Text="{Binding SiteName}" />
    </StackPanel>
</HierarchicalDataTemplate>

xaml和資源在工作查找之上並按預期進行查找。

我如何使用觸發器來擴展樣式定義,例如處理“ IsSelected”事件,以便所選的樹節點具有板岩灰色邊框和淺灰色背景?

研究: 我想要的東西。

更新: TreeView上沒有IsSelected屬性,但是TreeViewItem確實定義了一個。

嘗試這個:

  <DataTemplate x:Key="VolumeInfoDataTemplate"> 
        <StackPanel Orientation="Horizontal"> 
            <TextBlock Width="{TemplateBinding Width}" 
                       Height="{TemplateBinding Height}" 
                       Margin="5" 
                       Style="{DynamicResource TreeViewItemStyle_ChildNodes}" 
                       Text="{Binding VolumeName}"
                       Name="Tb" /> 
        </StackPanel> 
        <DataTemplate.Triggers>
             <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource    AncestorType=TreeViewItem}}" Value="True">
                  <Setter TargetName="Tb" Property="Background" Value="LightGray"/>
             </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM