如何在不命名绑定的情况下根据绑定为DataGridCell的内容设置样式 [英] How can I style a DataGridCell's content based on binding without naming that binding

查看:67
本文介绍了如何在不命名绑定的情况下根据绑定为DataGridCell的内容设置样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一种样式,使单元格的内容如果为正,则为绿色;如果为负,则为红色;如果为0,则为黑色.

I would like to create a style that makes my cell's content green if positive, red if negative or black if 0.

我了解转换器和绑定,但是是否可以在不命名特定列绑定到的字段的情况下做到这一点(例如,我将基于单元格的值)吗?

I know about converters and bindings, but is it possible to do this without naming the field the specific column is bound to (eg. I was to base on whatever is the cell's value)?

            <Style x:Key="GreenIfPositive" TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding, Converter={StaticResource greaterThanZeroDecimalConverter}}" Value="True">
                        <Setter Property="Foreground" Value="Green"/>
                    </DataTrigger>
                    <DataTrigger BBinding="{Binding, Converter={StaticResource greaterThanZeroDecimalConverter}}" Value="False">
                        <Setter Property="Foreground" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding}" Value="0">
                        <Setter Property="Foreground" Value="Black"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

这样我就可以在列上使用它,而无需再次重复该样式,只是我可以选择要基于此的属性.

So that I could use it on columns without re-iterating that style just so I can select the property I'm basing this on.

推荐答案

这是DataGridTextColumn的解决方案. DataGridTextColumn创建TextBlock元素以显示单元格值. TextBlock具有字符串Text属性.这些TextBlocks可以通过DataGridCell Content属性进行访问,因此生成的绑定路径为"Content.Text"

here is a solution for DataGridTextColumns. DataGridTextColumn creates TextBlock element to display cell value. TextBlock has string Text property. Those TextBlocks can be accessed via DataGridCell Content property, so resulting binding path is "Content.Text"

<Style.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
                 Path=Content.Text, Mode=OneWay,
                 Converter={StaticResource greaterThanZeroDecimalConverter}}" 
                 Value="True">
        <Setter Property="Foreground" Value="Green"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
                 Path=Content.Text, Mode=OneWay,
                 Converter={StaticResource greaterThanZeroDecimalConverter}}" 
                 Value="False">
        <Setter Property="Foreground" Value="Red"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
                 Path=Content.Text, Mode=OneWay}" 
                 Value="0">
        <Setter Property="Foreground" Value="Black"/>
    </DataTrigger>
</Style.Triggers>

请注意{RelativeSource Self}.

我还必须更改Convert方法,因为文本是字符串属性,传入值是字符串.

I also had to change Convert method because Text is a string property and incoming value is string.

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    double d;
    if (value != null && value is string && double.TryParse(value.ToString(), out d))
    {
        return d > 0;
    }
    return null;
}

这篇关于如何在不命名绑定的情况下根据绑定为DataGridCell的内容设置样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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