如何在WPFToolkit DataGrid中获取当前单元格的绑定值

时间:2009-03-26 15:01:21

标签: wpf data-binding

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

<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而不是我想要转换的数值。我希望这可以跨应用程序工作,而不需要为每个绑定指定正确的路径。

非常感谢!

4 个答案:

答案 0 :(得分:2)

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

对于处于相同情况的任何人,代码如下:

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简单需要小数?并根据其值将其转换为指定画笔之一。

答案 1 :(得分:0)

你也有什么控制风格?这听起来就像你正在应用它没有为自己设置任何特定的绑定,所以它只是继承其父项的值,最终成为你的ViewModel实例。

更新:根据评论,我认为您需要在样式的Binding表达式中指定Path。由于未指定路径,因此它仅使用当前的DataContext,最终成为整个ViewModel实例。

答案 2 :(得分:0)

好的,我找不到解决原始问题的方法,但是我会使用DataGridTemplateColumn解决它,模板可以根据绑定到它们的值正确设置Foreground颜色。

答案 3 :(得分:0)

您如何获取此代码来读取DataGrid本身的IsSelected属性?我已经尝试了以下代码,但无法解决如何将bool值放入ConverterParameter中,其中DecimalBrushConverter读取参数并提供SelectedBrush,如果isSelected == true。

public class DataGridDecimalColumn:DataGridTextColumn     {         private readonly DecimalBrushConverter _brushConverter = new DecimalBrushConverter                                                                      {                                                                          NegativeBrush = Brushes.Red,                                                                          PositiveBrush = Brushes.Black,                                                                          ZeroBrush = Brushes.Black,                                                                          SelectedBrush = Brushes.White                                                                      };

    private Binding _foregroundBinding;

    private DecimalBrushConverter BrushConverter
    {
        get { return _brushConverter; }
    }



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

        if (element != null)
            element.SetBinding(TextBlock.ForegroundProperty, GetForegroundBinding());

        return element;
    }



    private Binding GetForegroundBinding()
    {
        if (_foregroundBinding == null)
        {
            var binding = (Binding) Binding;
            var bindingToRow = new Binding
                                   {
                                       Path = new PropertyPath("IsSelected"),
                                       RelativeSource=new RelativeSource(RelativeSourceMode.FindAncestor,typeof(DataGridRow),1)
                                   };
            _foregroundBinding = new Binding
                                     {
                                         Path = binding.Path,
                                         Converter = BrushConverter,
                                         ConverterParameter = bindingToRow
                                     };
        }
        return _foregroundBinding;
    }
}