Color DataGridCell by Cellvalue

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 03:59:52

问题


i've a WPF DataGrid with different count of columns. I want to color the single cells dependent by the value. For example: If the cell-value is 0, then red.

These are my experiments:

<DataGrid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" x:Name="DataGrid"  SelectionUnit="Cell">
        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                  <!--experiment 1 -->
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Value, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" Value="0">
                        <Setter Property="Background" Value="LimeGreen"/>
                    </DataTrigger>
                   <!--experiment 2 -->
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Content, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" Value="0">
                        <Setter Property="Background" Value="LimeGreen"/>
                    </DataTrigger>
                    </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
</DataGrid>

回答1:


Just use a value converter (with the cell value as a parameter), that returns the color you want.

<DataGrid>
    <DataGridCell Background="{Binding CellValueField, Converter={StaticResource YourDefinedValueToColorConverter}}" />
</DataGrid>

EDIT: Finally got it to work.

Converter and style definitions:

<Window.Resources>
    <c:ValueToColorConverter x:Key="ValueToColorConverter"/>
    <Style x:Key="CellStyle" TargetType="DataGridCell">
        <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ValueToColorConverter}}" />
    </Style>
</Window.Resources>

The DataGrid:

<DataGrid HorizontalAlignment="Left"
              Margin="10,10,0,0"
              VerticalAlignment="Top"
              Loaded="DataGrid_Loaded">
        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="Background" Value="{Binding Converter={StaticResource ValueToColorConverter}}" />
            </Style>
        </DataGrid.CellStyle>
    </DataGrid>

And the converter:

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var cell = value as Order;
        if (cell != null && cell.Size > 80)
            return new SolidColorBrush(Colors.Red);
        else return new SolidColorBrush(Colors.Yellow);
    }

I used the DataGrid_Loaded method to fill the DataGrid with some random data encapsulated in a sample class:

class Order
{
    public int Size { get; set; }
}

And the result:




回答2:


Use a value converter, like this:

<DataGridCell Background="{Binding CellValueField, Converter={StaticResource IntegerToColorValueConverter}}" />

And:

public class IntegerToColorValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
          System.Globalization.CultureInfo culture)
    {
        switch ((int)value)
        {
            case 1: return Color.Red; break;
            case 2: return Color.Yellow; break;
            Default: return Color.White; break;
        }
    }

}


来源:https://stackoverflow.com/questions/29010317/color-datagridcell-by-cellvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!