在运行时更改WPF数据网格的整列的背景颜色 [英] Change the Background Color of Entire Column of WPF DataGrid at RunTime

查看:994
本文介绍了在运行时更改WPF数据网格的整列的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,我是比较新的WPF。我已经搜索周围的答案,但我发现是如何做到在运行时不列colorise行;例如下面的问题:

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:


  1. 更改WPF DataGrid行彩色

如何编程的方法修改DataGrid行的颜色在WPF?

WPF:编程分配颜色以DataGrid中排

基于价值观变化的DataGrid单元格颜色

等。

我见过的的 CellStyle 属性=htt​​p://msdn.microsoft.com /en-us/library/system.windows.controls.datagridtextcolumn.aspx\">MSDN DataGrid的页面的,但它的用途并不明显,我的一切,尽管这周围以及搜索。

I have seen the CellStyle property on the MSDN DataGrid pages but its use is not obvious to me at all despite searches around this as well.

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

感谢您的时间。

推荐答案

我得到它的工作的唯一办法是通过自己设置的列,(不使用自动生成)。因此,首先要做的是定义列:

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> 



然后,你需要设置每列CellStyle并绑定背景,你可以在声明静态资源Window.Resources:

Then you need to set each column CellStyle and bind the Background to a static resource that you can declare at 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>



那么你可以操纵通过代码或XAML操作的静态资源。

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

希望它帮助。

这篇关于在运行时更改WPF数据网格的整列的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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