2012-02-17 5 views
1

ログイベントを表示するListViewがあります。スタイルトリガーは、ログの重大度に応じて行の色を変更するように定義されています。 CellTemplateを使用する1つの列を除いて正常に動作します。ラベルアイテムは通常の色を保ちます。例えばGridViewのセルテンプレート列でスタイルトリガが動作しないのはなぜですか?

<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> 

レベル=デバッグ場合、 'Class.Method' 列には、適切な背景(LightGrey)を受信するが、Forgroundは黒(デフォルト)のまま。

ここで何をすればいいですか? x:型ラベルのStyle.Triggersを繰り返しますか?

答えて

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 ... 
関連する問題