样式触发器的列表视图子项

本文关键字:视图 列表 触发器 样式 | 更新日期: 2023-09-27 18:05:03

我有一个ListView,它是数据绑定到基于某些属性的项目行颜色代码。

我想重写一个特定的单元格,以根据不同的属性更改其颜色。

ListView ....

<Grid>
    <ListView x:Name="dbItems" ItemsSource="{Binding itemList}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
        <ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Background" Value="{Binding connectionStatus, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource converter}}" />
            </Style>
            <!--<Style TargetType="{x:Type ListViewItem.}">
                <Setter Property="Background" Value="{Binding badSubItem, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource cellConverter}}" />
            </Style>-->
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                <GridViewColumn Header="DESCRIPTION" DisplayMemberBinding="{Binding description}" />
                <GridViewColumn Header="RESOURCE" DisplayMemberBinding="{Binding resource}" />
                <GridViewColumn Header="SubItem" DisplayMemberBinding="{Binding subItemFound}" />
                <GridViewColumn Header="Count" DisplayMemberBinding="{Binding count}"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

每行的初始颜色编码由Converter处理。

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
        switch ((Interfaces.Enum.Status)value)
        {
            case Interfaces.Enum.Status.Unknown:
                return Brushes.LightGray;
            case Interfaces.Enum.Status.Offline:
                return Brushes.Red;
            case Interfaces.Enum.Status.Online:
                return Brushes.Green;
            case Interfaces.Enum.Status.OnlineButNotInitialized:
                return Brushes.OrangeRed;
            default:
                return Brushes.LightGray;
        }
}

细胞的颜色变化,我希望使用另一个Converter

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!(bool)value)
            return Brushes.LightGreen;
        return Brushes.Yellow;
    }

问题是在注释出来的风格标签(第二个ListViewItem风格),因为这需要只适用于特定的单元格(子项- bool),但我似乎找不到正确的绑定类型。有没有人能在这方面帮助我,或者给我指出正确的方向来完成这件事……

样式触发器的列表视图子项

IsSomething替换为要绑定到

的属性
<Style x:Key="MyDataGridCell" TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
            <Setter Property="Background" Value="White"/>
            <Setter Property="Foreground" Value="Black"/>
    <Style.Triggers>
        <Trigger Property="{Binding IsSomething}" Value="True">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>
<DataGrid CellStyle="{StaticResource MyDataGridCell}" />