WPF列表视图。如何同时支持此行的行背景颜色和alternateIndex

时间:2013-01-10 19:56:19

标签: wpf xaml listview triggers

我有一个包含日志消息的listview。我想根据相应条目的严重性为listview中的每一行设置背景颜色。我使用DataTrigger (参见示例)

执行此操作

我还要为AlternationIndex支持listview。 如何在 xaml DataTrigger abd Trigger 中将它们组合成行的背景颜色?

对于第I行的设置背景颜色,请使用以下代码:

<ListView ... >
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Severity} Value="Info">

                   <Setter 
                     Property="Background" 
                     Value="{Binding Path=Severity,
                             Converter=
                               {StaticResource msgSeverityToColorConverter}}" 
                   />

                 </DataTrigger>

                 <DataTrigger Binding="{Binding Path=Severity} Value="Error">

                   <Setter 
                     Property="Background" 
                     Value="{Binding Path=Severity, 
                             Converter=
                            {StaticResource msgSeverityToColorConverter}}"
                    />

                 </DataTrigger>

            </Style.Triggers>

        </Style>

    </ListView.ItemContainerStyle>

</ListView>

对于AlternateIndex我有以下代码:

<Style.Triggers>
    <Trigger Property="ItemsControl.AlternationIndex" Value="1">

        <Setter Property="Background" Value="LightBlue"></Setter>

     </Trigger>

    <Trigger Property="ItemsControl.AlternationIndex" Value="2">

   <Setter Property="Background" Value="LightGray"></Setter>

    </Trigger>

 </Style.Triggers>

当有消息的行不是InfoError时,我需要它,它将是来自AlternationIndex属性的颜色。

1 个答案:

答案 0 :(得分:0)

这完全归功于触发器声明的顺序。首先声明交替触发器,然后声明严重性触发器。

<Style.Triggers>
    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
        <Setter Property="Background" Value="LightBlue"></Setter>
    </Trigger>
    <Trigger Property="ItemsControl.AlternationIndex" Value="2">
       <Setter Property="Background" Value="LightGray"></Setter>
    </Trigger>
    <DataTrigger Binding="{Binding Path=Severity} Value="Info">
        <Setter Property="Background" 
                Value="{Binding Path=Severity,
                Converter="{StaticResource msgSeverityToColorConverter}}" />
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=Severity} Value="Error">
        <Setter Property="Background" 
                Value="{Binding Path=Severity, 
                Converter="{StaticResource msgSeverityToColorConverter}}" />
    </DataTrigger>
</Style.Triggers>