listview子项

时间:2015-07-28 12:24:43

标签: c# wpf data-binding converter

我有一个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样式)中,因为它只需要应用于特定的单元格(subitem - bool),但我似乎无法找到正确的绑定类型。任何人都可以帮助我,或指出我正确的方向来完成这件事......

1 个答案:

答案 0 :(得分:0)

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}" />