当与DataGrid一起使用时,{RelativeSource PreviousData}始终为NULL

4
我想在DataGrid中使用{RelativeSource PreviousData},并将该值用于转换器。但是,在DataGrid中它总是给出null值。然而,在ListBox中它工作得非常完美。我们需要做更多的事情来让它与DataGrid一起工作吗?
1个回答

0

DataGridTemplateColumn不属于DataGrid的视觉或逻辑树。可以按照Thomas Levesque提出的解决方案使用Freezable类来解决这个问题。

public class BindingProxy : Freezable
{

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }


    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.  This enables    animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}

在DataGrid的资源中声明此类的实例,并将Data属性绑定到当前DataContext。
<DataGrid.Resources>
    <local:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>

最后一步是将这个BindingProxy对象指定为绑定的源。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接