【问题标题】:Why aren't style triggers operating on cell templated columns in GridView?为什么样式触发器不在 GridView 中的单元格模板列上运行?
【发布时间】:2012-03-08 20:46:14
【问题描述】:

我有一个 ListView 来显示日志事件。定义样式触发器以根据日志严重性更改行的颜色。工作正常,除了一列使用 CellTemplate。标签项保持正常颜色。

<ListView x:Name="LogEvents" Grid.Row="0" FontSize="9">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Background" Value="White"/>
            <Setter Property="Foreground" Value="Black"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Level.DisplayName}" Value="ERROR">
                    <Setter Property="Background" Value="LightSalmon"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Level.DisplayName}" Value="INFO">
                    <Setter Property="Background" Value="LightGreen"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Level.DisplayName}" Value="DEBUG">
                    <Setter Property="Background" Value="White"/>
                    <Setter Property="Foreground" Value="Gray"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Level.DisplayName}" Value="WARN">
                    <Setter Property="Background" Value="LightYellow"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Level.DisplayName}" Value="FATAL">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.View>
        <GridView x:Name="GridView1">
            <GridViewColumn Header="TimeStamp" DisplayMemberBinding="{Binding TimeStamp, ConverterCulture=de-DE, StringFormat=HH:mm:ss.fff}"/>
            <GridViewColumn  DisplayMemberBinding="{Binding Level}" Header="Level" />
            <GridViewColumn Header="Thread" DisplayMemberBinding="{Binding ThreadName}"  />
            <GridViewColumn Header="Message" DisplayMemberBinding="{Binding RenderedMessage}"  />
            <!--<GridViewColumn Header="UserName" DisplayMemberBinding="{Binding UserName}"  />-->
            <GridViewColumn Header="Class.Method">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Label Content="{Binding Path=LocationInformation.ClassName}" Padding="0"/>
                            <Label Content="." Padding="0"/>
                            <Label Content="{Binding Path=LocationInformation.MethodName}" Padding="0"/>
                            <Label Content="(" Padding="0"/>
                            <Label Content="{Binding Path=LocationInformation.LineNumber}" Padding="0"/>
                            <Label Content=")" Padding="0"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

例如,如果 Level=Debug,则“Class.Method”列接收适当的背景 (LightGrey),但前景保持黑色(默认)。

我必须在这里做什么?为 x:Type 标签重复 Style.Triggers?

【问题讨论】:

    标签: wpf triggers


    【解决方案1】:

    这行得通。不确定是否有更好的方法,但它相当紧凑。

    <GridViewColumn.CellTemplate>
         <DataTemplate>
            <!-- Added this section so Label inherits from Parent ListViewItem -->
            <DataTemplate.Resources>
                <Style TargetType="{x:Type Label}">
                    <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=Background}"/>
                    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=Foreground}"/>
                </Style>
            </DataTemplate.Resources>
            <StackPanel Orientation="Horizontal">
               <Label  ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多