如何在不命名该绑定的情况下根据绑定设置

如何在不命名该绑定的情况下根据绑定设置

本文介绍了如何在不命名该绑定的情况下根据绑定设置 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 属性.可以通过 DataGridCell Content 属性访问这些 TextBlock,因此生成的绑定路径为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 方法,因为 Text 是一个字符串属性,传入的值是字符串.

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 内容的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:58