WPF DataGrid单元的条件格式取决于DataType [英] Conditional Formatting of WPF DataGrid Cell Depending on DataType

查看:111
本文介绍了WPF DataGrid单元的条件格式取决于DataType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件,该控件继承自WPF DataGrid控件,并且向我传递了不同的对象列表,因此,网格的列取决于(即自动生成)我通过其发送的对象类。

I have a user control that inherits from the WPF DataGrid control and to which I pass different List of objects and, so, the columns of the grid depend (i.e. are automatically generated) on the class of object that I send through.

<userc:cfDataGrid x:Name="grdResults" 
    FontStyle="Normal" 
    FontFamily="Microsoft Sans Serif" 
    FontSize="14" 
    RowHeight="30"  

    AutoGenerateColumns="True" 
    Margin="12" 
    AlternatingRowBackground="Gainsboro"  
    AlternationCount="2" 

    ItemsSource="{Binding Results}" 
    IsReadOnly="True" 
    HeaderNames="{Binding Headers}" >
</userc:cfDataGrid> 

现在,我还要通过具有SolidColorBrush数据属性的对象列表类型,并能够使用此值设置单元格的背景颜色,而不是显示实际值。

Now, I'd like to also pass through a list of objects that have an attribute of SolidColorBrush data type and be able to use this value to set the background colour of the cell rather than display the actual value.

我还没有对样式等做很多工作,所以我想问问是否有人可以给我一些关于如何使用SolidColorBrush数据类型捕获列的指针-并执行

I have not done much work with styles etc and so I wanted to ask if anyone could give me some pointers on how to trap a column with a SolidColorBrush datatype - and perform the action described above - without depending on the name of the column?

任何指针将不胜感激!

CC

推荐答案

我将使用使用 DataTrigger href = http://msdn.microsoft.com/zh-cn/library/system.windows.data.ivalueconverter.aspx rel = noreferrer>转换器检查对象是否为 SolidColorBrush ,然后设置背景色

I would use a DataTrigger that used a Converter to check if the object was a SolidColorBrush, and if so set the background color

类似这样的东西:

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <!-- DataContext will be object for entire data row -->
            <DataTrigger Binding="{Binding MyBrushColorProperty, Converter={StaticResource IsSolidBrushColor}}" Value="True">
                <Setter Property="Background" Value="{Binding MyBrushColorProperty}" />
                <Setter Property="Content" Value="" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.Resources>

这篇关于WPF DataGrid单元的条件格式取决于DataType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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