【发布时间】: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?
【问题讨论】: