C# 从CellStyle到DataGrid上下文的WPF绑定

C# 从CellStyle到DataGrid上下文的WPF绑定,c#,wpf,data-binding,datagrid,C#,Wpf,Data Binding,Datagrid,我有一个WPF DataGrid,它绑定到一个可观察的RowObject集合,该集合具有一系列可绑定的属性。为了填充表中的数据,我添加了绑定到RowObjects属性的DataGridTextColumns。例如: <DataGrid ItemsSource={Binding RowCollection}> <DataGrid.Columns> <DataGridTextColumn Header="Col1" Binding={Bindin

我有一个WPF DataGrid,它绑定到一个可观察的RowObject集合,该集合具有一系列可绑定的属性。为了填充表中的数据,我添加了绑定到RowObjects属性的DataGridTextColumns。例如:

<DataGrid ItemsSource={Binding RowCollection}>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Col1" Binding={Binding Property1Name, Mode=OneTime} IsReadOnly="True" />
        <DataGridTextColumn Header="Col2" Binding={Binding Property2Name, Mode=OneTime} IsReadOnly="True" />
        <DataGridTextColumn Header="Col3" Binding={Binding Property3Name, Mode=OneTime} IsReadOnly="True" />
    </DataGrid.Columns>
</DataGrid>


假设Property3是一个整数。我希望第3列中的单元格在它们为负时高亮显示为红色,在为零时高亮显示为黄色,在为正时高亮显示为绿色。我的第一个想法是将System.Windows.Media.Color绑定到DataGridTextColumn的CellStyle,但这似乎无法直接工作。有什么想法吗?

这不容易,但您可以对每个单元格背景使用转换器

一个单元格的样式:

<Style x:Key="Col1DataGridCell" TargetType="DataGridCell">
    <Setter Property="Background" Value="{Binding Converter={StaticResource Col1Converter}}" />
</Style>
public class Col1Converter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        var result = (RowObject)value;

        Color color;
        if (result.Value < 0) {
            color = Colors.Red;
        }
        else if (result.Value == 0) {
            color = Colors.Yellow;
        }
        else {
            color = Colors.Green;
        }

        return new SolidColorBrush(color);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}

一个电池的转换器:

<Style x:Key="Col1DataGridCell" TargetType="DataGridCell">
    <Setter Property="Background" Value="{Binding Converter={StaticResource Col1Converter}}" />
</Style>
public class Col1Converter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        var result = (RowObject)value;

        Color color;
        if (result.Value < 0) {
            color = Colors.Red;
        }
        else if (result.Value == 0) {
            color = Colors.Yellow;
        }
        else {
            color = Colors.Green;
        }

        return new SolidColorBrush(color);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}
公共类Col1Converter:IValueConverter{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性){
var结果=(RowObject)值;
颜色;
如果(结果值<0){
颜色=颜色。红色;
}
else if(result.Value==0){
颜色=颜色。黄色;
}
否则{
颜色=颜色。绿色;
}
返回新的SolidColorBrush(颜色);
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性){
抛出新的NotImplementedException();
}
}
在DataGrid中使用:

<DataGridTextColumn Header="Col1" Style={StaticResource Col1DataGridCell} Binding={Binding Property1Name, Mode=OneTime} IsReadOnly="True" />

我建议您使用能够在IValueConverter的帮助下改变电池颜色的样式

检查这个:并进行实验


祝你好运。

我提交了几乎相同的解决方案,晚了17秒……这次你赢了