【问题标题】:{RelativeSource PreviousData} always NULL when used with DataGrid{RelativeSource PreviousData} 与 DataGrid 一起使用时始终为 NULL
【发布时间】:2015-03-22 04:04:42
【问题描述】:

我想将 {RelativeSource PreviousData} 与 Datagrid 一起使用。我想在转换器中使用该值。但是对于 DataGrid,它总是给出空值。而它与 ListBox 完美配合。我们还需要什么才能让它与 DataGrid 一起使用吗?


【问题讨论】:

    标签: wpf datagrid


    【解决方案1】:

    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 对象指定为绑定的 Source。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-01
      • 2011-10-15
      • 2017-07-15
      • 2018-02-16
      • 2019-01-12
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      相关资源
      最近更新 更多