首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >彩色DataGridCell

彩色DataGridCell
EN

Stack Overflow用户
提问于 2015-03-12 12:52:46
回答 2查看 1.8K关注 0票数 1

我有一个WPF DataGrid,具有不同的列计数。我想用这个值来给单细胞着色。例如:如果单元格值为0,则为红色。

这些是我的实验:

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-12 13:48:58

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

代码语言:javascript
复制
<DataGrid>
    <DataGridCell Background="{Binding CellValueField, Converter={StaticResource YourDefinedValueToColorConverter}}" />
</DataGrid>

编辑:终于让它开始工作了。

转换器和样式定义:

代码语言:javascript
复制
<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>

DataGrid:

代码语言:javascript
复制
<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>

转换器:

代码语言:javascript
复制
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);
    }

我使用DataGrid_Loaded方法将封装在示例类中的一些随机数据填充到DataGrid中:

代码语言:javascript
复制
class Order
{
    public int Size { get; set; }
}

其结果是:

票数 0
EN

Stack Overflow用户

发布于 2015-03-12 14:01:28

使用值转换器,如下所示:

代码语言:javascript
复制
<DataGridCell Background="{Binding CellValueField, Converter={StaticResource IntegerToColorValueConverter}}" />

和:

代码语言:javascript
复制
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;
        }
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29010317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档