0

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

我也想AlternationIndex支持listview. 如何将它们组合成行xaml DataTrigger abd Trigger的背景颜色?

对于为行设置背景颜色,我使用以下代码:

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

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

4

1 回答 1

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>
于 2013-01-10T20:20:35.537 回答