如何获取WPFToolkit DataGrid中当前单元格的绑定值 [英] How to get Binding value of current cell in a WPFToolkit DataGrid

查看:335
本文介绍了如何获取WPFToolkit DataGrid中当前单元格的绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改保存负数的单元格的前景色,但是我不知道如何指定允许我的DataTrigger。我使用这样的东西:

I want to change the foreground color of cells that hold negative numbers, but I don't know how to specify the DataTrigger that would let me. I'm using something like this:

<Style x:Key="NumberCellStyle" BasedOn="{StaticResource CellStyle}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Converter={StaticResourceExtension SignConverter}}" Value="-1">
            <Setter Property="TextBlock.Foreground" Value="Red"/> 
        </DataTrigger>
    </Style.Triggers>
</Style>

但是在SignConverter转换器中,我得到整个ViewModel,而不是我要转换的数值。我希望这可以在整个应用程序中工作,而不需要为每个绑定指定正确的路径。

But in the SignConverter converter I get the whole ViewModel instead of the numeric value I want to convert. I want this to work across the app, without me needing to specify the correct Path for each binding.

非常感谢!

推荐答案

更好的方法,写一个自定义列。

Better way, write a custom column.

代码符合相同情况的任何人:

The code follows for anyone that's in the same situation:

public class DataGridDecimalColumn : DataGridTextColumn
{
    Binding               foregroundBinding;
    DecimalBrushConverter brushConverter = new DecimalBrushConverter {
        NegativeBrush = Brushes.Red, 
        PositiveBrush = Brushes.Black, 
        ZeroBrush     = Brushes.Black,
    };

    protected override FrameworkElement 
    GenerateElement(DataGridCell cell, object dataItem)
    {
        var element = base.GenerateElement(cell, dataItem) as TextBlock;
        element.SetBinding(TextBlock.ForegroundProperty, GetForegroundBinding());
        return element;
    }

    Binding
    GetForegroundBinding()
    {
        if(foregroundBinding == null) {
            var binding       = (Binding)Binding;
            foregroundBinding = new Binding {
                Path      = binding.Path,
                Converter = BrushConverter,
            };
        }
        return foregroundBinding;
    }

    public DecimalBrushConverter 
    BrushConverter
    {
        get { return brushConverter; }
        set { brushConverter = value; }
    }
}

DecimalBrushConverter simple是一个小数?并根据其值将其转换为指定的刷子之一。

DecimalBrushConverter simple takes a decimal? and converts it to one of the specified brushes depending on its value.

这篇关于如何获取WPFToolkit DataGrid中当前单元格的绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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