C# 在运行时更改 WPF DataGrid 整个列的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15644105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:27:32  来源:igfitidea点击:

Change the Background Color of Entire Column of WPF DataGrid at RunTime

c#wpfdatagridcolors

提问by MoonKnight

All, I am relatively new to WPF. I have searched around for the answer to this, but all I have found is how to do colorise rows at run-time not columns; for example the following questions:

所有,我对 WPF 比较陌生。我四处寻找答案,但我发现的只是如何在运行时对行进行着色,而不是对列进行着色;例如以下问题:

  1. Change WPF Datagrid Row Color

  2. How do I programatically change datagrid row color in WPF?

  3. Programmatically assigning a color to a row in DataGrid

  4. Change DataGrid cell colour based on values

  1. 更改 WPF 数据网格行颜色

  2. 如何以编程方式更改 WPF 中的数据网格行颜色?

  3. 以编程方式为 DataGrid 中的行分配颜色

  4. 根据值更改 DataGrid 单元格颜色

et al.

等。

I have seen the CellStyleproperty on the MSDN DataGrid pagesbut its use is not obvious to me at all despite searches around this as well.

我已经CellStyleMSDN DataGrid 页面上看到了该属性,但尽管也围绕此进行了搜索,但它的使用对我来说根本不明显。

how to change the background colour of an entire column at runtime?

如何在运行时更改整个列的背景颜色?

Thanks for your time.

谢谢你的时间。

采纳答案by Matan Shahar

The only way I got it to work is by setting the columns by myself, (by not using AutoGenerate). So first thing to do is define the columns:

我让它工作的唯一方法是自己设置列(不使用自动生成)。所以首先要做的是定义列:

<DataGrid x:Name="Frid" ItemsSource="{Binding Path=.}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name" 
                                Binding="{Binding Path=FirstName}">

            </DataGridTextColumn>

            <DataGridTextColumn Header="Last Name" 
                                Binding="{Binding Path=LastName}">

            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid> 

Then you need to set each column CellStyle and bind the Background to a static resource that you can declare at Window.Resources:

然后你需要设置每一列 CellStyle 并将 Background 绑定到你可以在 Window.Resources 声明的静态资源:

<Window x:Class="WpfApplication1.MainWindow" ...>
<Window.Resources>
    <SolidColorBrush x:Key="clBr" Color="White" />
</Window.Resources>
...

Columns:

列:

                <DataGridTextColumn Header="First Name" 
                                    Binding="{Binding Path=FirstName}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" 
                                Value="{StaticResource clBr}" />
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

then you can just manipulate the static resource by either code or xaml manipulation.

那么您可以通过代码或 xaml 操作来操作静态资源。

Hope it helps.

希望能帮助到你。

回答by dotNET

A bit old, but here is how you can do this programmatically (for AutoGen columns):

有点旧,但这里是如何以编程方式执行此操作(对于 AutoGen 列):

private void dgvMailingList_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    e.Column.CellStyle = new Style(typeof(DataGridCell));
    e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.BackgroundProperty,  new SolidColorBrush(Colors.LightBlue)));
}

The same approach can be applied to non-AutoGen columns too.

同样的方法也可以应用于非 AutoGen 列。