彩色DataGridCell由Cellvalue [英] Color DataGridCell by Cellvalue

查看:182
本文介绍了彩色DataGridCell由Cellvalue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不同列数的WPF DataGrid。我想对依赖于值的单个单元进行着色。
例如:如果单元格值为0,则为红色。



这些是我的实验:

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


解决方案

只需使用值转换器(单元格值为一个参数),返回你想要的颜色。

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






编辑:最后让它工作。 p>

转换器和样式定义:

 < Window.Resources> 
< c:ValueToColorConverter x:Key =ValueToColorConverter/>
< Style x:Key =CellStyleTargetType =DataGridCell>
< Setter Property =BackgroundValue ={Binding RelativeSource = {RelativeSource Self},Converter = {StaticResource ValueToColorConverter}}/>
< / Style>
< /Window.Resources>

DataGrid:

 < DataGrid Horizo​​ntalAlignment =Left
Margin =10,10,0,0
VerticalAlignment =Top
Loaded =DataGrid_Loaded>
< DataGrid.CellStyle>
< Style TargetType =DataGridCell>
< Setter Property =BackgroundValue ={Binding Converter = {StaticResource ValueToColorConverter}}/>
< / Style>
< /DataGrid.CellStyle>
< / DataGrid>

转换器:

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

我使用了 DataGrid_Loaded 方法用一些随机数据封装在样本类中来填充DataGrid:

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

结果:




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>

解决方案

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:

这篇关于彩色DataGridCell由Cellvalue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆