【问题标题】:Cannot bind ObjectContext from DataContext to custom Attached Property无法将 DataContext 中的 ObjectContext 绑定到自定义附加属性
【发布时间】:2011-07-23 07:57:22
【问题描述】:

我已经附加了财产

    public static ObjectContext GetObjectContext(DataGrid obj)
    {
        return (ObjectContext)obj.GetValue(ObjectContextProperty);
    }

    public static void SetObjectContext(DataGrid obj, ObjectContext value)
    {
        obj.SetValue(ObjectContextProperty, value);
    }

    public static readonly DependencyProperty ObjectContextProperty =
        DependencyProperty.RegisterAttached("ObjectContext", typeof(ObjectContext), typeof(FilterableGrid));

我还有 ViewModel,它的 Context 属性设置为正确的 ObjectContext(用调试器检查)。

还有这个 XAML 代码:

<DataGrid AutoGenerateColumns="False" Name="gridOrders"
              local:FilterableGrid.ObjectContext="{Binding Context}" local:FilterableGrid.IsFilterableGrid="True">

local 是我的命名空间。

我在 IsFilterableGrid chaged 事件(附加行为)中使用此代码来检索对象上下文:

ObjectContext context = FilterableGrid.GetObjectContext(sourceGrid);

而且上下文变量总是以 null 结束。虽然网格的 DataContext 不为 null 并且指向正确的对象(ViewModel)。

有什么想法吗?我似乎对此失去了理智......

编辑:我进行了更多调查,如果我绑定local:FilterableGrid.IsFilterableGrid="{Binding IsFilterable}",它会完美运行。但是local:FilterableGrid.ObjectContext="{Binding Context}" 没有!也许它与传递的值的类型有关(ObjectContext 而不是 string 或者说 bool,它们是原始类型?)?

【问题讨论】:

    标签: c# wpf binding attached-properties attachedbehaviors


    【解决方案1】:

    我终于解决了这个问题,甚至确定有什么帮助 - 似乎这是 .NET 或 VS 中的一种错误。

    我所做的是:向 RegisterAttached 调用添加了一些属性元数据,并添加了 OnChanged 委托。之后它才开始工作(正确绑定)。然后,当我从 RegisterAttached 调用中删除属性元数据时,它继续工作。

    所以我真的不知道它是什么,但现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-24
      • 2020-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2011-01-21
      • 2017-01-25
      相关资源
      最近更新 更多