如何更改WPF DataGrid中某些单元格的背景颜色? [英] How to change background color for some cell in WPF DataGrid?

查看:80
本文介绍了如何更改WPF DataGrid中某些单元格的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到对象集合的数据网格.

I have a datagrid binded to an object collection.

对象的一个​​属性是存储颜色值的字符串.

One property of the objects is a string that store a color value.

在此"COLORBACKGROUND"单元格上单击,将打开一个颜色选择器以对其进行更改.

On this "COLORBACKGROUND" cell click, a colorpicker opens to change it.

我需要通过datagrid行(#RGB)中显示的值更改单元格的背景颜色.

What I need is to change the background color of the cell by the value displayed in datagrid rows (#RGB).

<DataGrid SelectionUnit="Cell" SelectedCellsChanged="DgDataTable_OnSelectedCellsChanged" x:Name="DgDataTable" Grid.Row="0" Grid.ColumnSpan="2" Margin="10,20,10,0" AutoGenerateColumns="true" HeadersVisibility="All" RowHeaderWidth="20" Style="{StaticResource AzureDataGrid}" GridLinesVisibility="Horizontal" LoadingRow="dgDataTable_LoadingRow" ColumnHeaderHeight="10" AlternatingRowBackground="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" AutoGeneratingColumn="DgDataTable_AutoGeneratingColumn">
    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="Background" Value="LightCyan"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </DataGrid.RowHeaderStyle>
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="TextBlock.TextAlignment" Value="Center"/>
          <!--  <Style.Triggers>
                <Trigger Property="Text" Value="John">
                    <Setter Property="Background" Value="LightGreen"/>
                </Trigger>
            </Style.Triggers> -->
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

我尝试使用自动生成"列进行操作:

I tried something with AutoGenerating column :

private void DgDataTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName == "SrcAlert")
    {
        DataGridComboBoxColumn cb = new DataGridComboBoxColumn();
        e.Column = cb;
        cb.ItemsSource = new List<string> {"1", "2"};
        cb.SelectedValueBinding = new Binding("SrcAlert");
        e.Column.Header = "SrcAlert";
    }
    if (e.PropertyName.Equals("ColorBackground"))
    {
        DataGridTextColumn tc = new DataGridTextColumn();
        e.Column = tc;
        tc.Foreground = (Color)ColorConverter.ConvertFromString(DgDataTable.CurrentCell.Item.ColorBackground);
    }
}

Item.ColorBackground 无法编译...我将其用作解释,这就是我所需要的.

this Item.ColorBackground doesn't compile... I put it for my explanation, thats what I need.

我尝试了另一种发现的解决方案:

I tried another solution I found :

if (e.PropertyName.Equals("ColorBackground"))
{
    string s = DgDataTable.CurrentCell.Item.ToString();
    e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty, (Color)ColorConverter.ConvertFromString(s)));
}

但这是失败的.

谢谢您的帮助!


ASh解决方案的屏幕截图非常适合我:


Edit : A screenshot of ASh solution that works perfectly for me:

我使用颜色选择器将您的解决方案改编为多列:

EDIT : I adapted your solution for multiple columns with color Picker :

我添加样式设置器以仅显示单元格中的颜色:

I add style setters to display only colors in the cells :

    <Style TargetType="DataGridCell" x:Key="ColorPickerCellBG" 
       BasedOn="{StaticResource CommonCell}">
        <Setter Property="Background" Value="{Binding Path=BG}"/>
        <Setter Property="Foreground" Value="Transparent"/>
        <Setter Property="Width" Value="30"></Setter>
    </Style>
    <Style TargetType="DataGridCell" x:Key="ColorPickerCellAL" 
       BasedOn="{StaticResource CommonCell}">
        <Setter Property="Background" Value="{Binding Path=AL}"/>
        <Setter Property="Foreground" Value="Transparent"/>
        <Setter Property="Width" Value="30"></Setter>
    </Style>
    <Style...

单击单元格时,rgb颜色值可见,样式必须为"ClickedCell" ...我该如何改善呢?

When the cell is clicked, the rgb color value is visible, the style must be "ClickedCell"... How can I Improve that ?

推荐答案

可以将特殊样式应用于单个自动生成的列.

it is possible to apply special style to a single auto-generated column.

在资源中声明两种单元格样式

declare two cell styles in resources

<Window.Resources>
    <Style TargetType="DataGridCell" x:Key="CommonCell"
           BasedOn="{StaticResource {x:Type DataGridCell}}">
        <Setter Property="TextBlock.TextAlignment" Value="Center"/>
    </Style>

    <Style TargetType="DataGridCell" x:Key="ColorPickerCell" 
           BasedOn="{StaticResource CommonCell}">
        <Setter Property="Background" Value="{Binding Path=ColorBackground}"/>
    </Style>
</Window.Resources>

ColorPickerCell 继承了 CommonCell 样式.

<DataGrid SelectionUnit="Cell" 
          x:Name="DgDataTable" 
          AutoGenerateColumns="true" HeadersVisibility="All" RowHeaderWidth="20" 
          GridLinesVisibility="Horizontal" 
          ColumnHeaderHeight="10" 
          AlternatingRowBackground="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" 

          CellStyle="{StaticResource CommonCell}"
          AutoGeneratingColumn="DgDataTable_AutoGeneratingColumn">

    <DataGrid.RowHeaderStyle>
        <Style TargetType="DataGridRowHeader">
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="Background" Value="LightCyan"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </DataGrid.RowHeaderStyle>

</DataGrid>

为生成的列更改 CellStyle :

private void DgDataTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName == "ColorBackground")
    {
        e.Column.CellStyle = (sender as DataGrid).FindResource("ColorPickerCell") as Style;
    }
}

这篇关于如何更改WPF DataGrid中某些单元格的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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