我有一个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”列会收到相应的Background(LightGrey),但Forground仍为Black(默认值)。
我该怎么办?重复Style.Triggers for x:Type Labels?
答案 0 :(得分:0)
这很有用。不确定是否有更好的方式,但它相当紧凑。
<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 ...