c# WPF Tree View Item 条件样式

c# WPF Tree View Item conditional styling

如何根据绑定属性treeviewitems有条件地设置样式

我 运行 遇到了一个问题,我想在树视图中设置项目的样式,那些是有效选择的项目与层次结构项目。

我已经尝试将我的 style 标签移动到 TreeView.ResourcesTreeView.ItemContainerStyle 标签中,但它仍然不会导致任何格式设置。

<TabItem Name="Queries" Header="Queries" 
         d:DataContext="{d:DesignInstance views:QuerySettingsView}">
  <Grid>
    <TreeView ItemsSource="{Binding QueryTreeModels}" 
              Width="500" Height="200" VerticalAlignment="Top" 
              HorizontalAlignment="Center" Margin="0,0,175,0">
      <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
          <HierarchicalDataTemplate.ItemContainerStyle>
            <Style >
              <Setter Property="TreeViewItem.IsExpanded" 
                      Value="{Binding IsExpanded, Mode=TwoWay}" />
              <Setter Property="TreeViewItem.IsSelected" 
                      Value="{Binding IsSelected, Mode=TwoWay}" />
              <Style.Triggers>
                <DataTrigger Binding="{Binding IsQuery}">
                  <Setter Property="TextBlock.Foreground" Value="Chartreuse" />
               </DataTrigger>
               <DataTrigger Binding="{Binding IsFolder}">
                 <Setter Property="TextBlock.Foreground" Value="Crimson" />
               </DataTrigger>
             </Style.Triggers>
           </Style>
         </HierarchicalDataTemplate.ItemContainerStyle>
         <TextBlock Text="{Binding Name}"/>
       </HierarchicalDataTemplate>
     </TreeView.ItemTemplate>
   </TreeView>
 </Grid>
</TabItem>

对于 DataTrigger,您需要通过向 DataTrigger 绑定字段添加值来完成条件。喜欢 <DataTrigger Binding="{Binding IsQuery}" Value="true"> 参考下面的代码。

<TreeView x:Name="tree" Width="500" Height="200"
          VerticalAlignment="Top" HorizontalAlignment="Center"
          >
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <HierarchicalDataTemplate.ItemContainerStyle>                       
                    <Style >
                        <Setter Property="TreeViewItem.IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                        <Setter Property="TreeViewItem.IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsQuery}" Value="true">
                                <Setter Property="TreeViewItem.Foreground" Value="Chartreuse" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsFolder}" Value="true">
                                <Setter Property="TreeViewItem.Foreground" Value="Crimson" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </HierarchicalDataTemplate.ItemContainerStyle>
                <TextBlock Text="{Binding NodeName}"/>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>