无法将 ObjectContext 从 DataContext 绑定到自定义附加属性
我已经附加了属性
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,它具有设置为正确的 ObjectContext 的 Context 属性(使用调试器检查)。
这个 XAML 代码:
<DataGrid AutoGenerateColumns="False" Name="gridOrders"
local:FilterableGrid.ObjectContext="{Binding Context}" local:FilterableGrid.IsFilterableGrid="True">
其中 local 是我的命名空间。
我在 IsFilterableGrid chaged 事件(附加行为)中使用此代码来检索对象上下文:
ObjectContext context = FilterableGrid.GetObjectContext(sourceGrid);
并且上下文变量始终最终为空。尽管网格的 DataContext 不为 null 并且指向正确的对象(ViewModel)。
有什么想法吗?我似乎对此失去了理智......
编辑:我进行了更多调查,如果我绑定 local:FilterableGrid.IsFilterableGrid="{Binding IsFilterable}"
它工作完美。但 local:FilterableGrid.ObjectContext="{Binding Context}"
却没有!也许它与传递的值的类型有关(ObjectContext 而不是 string 或 bool,它们是原始类型?)?
I have attached property
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));
And I also have ViewModel that has Context property that is set to correct ObjectContext (checked with debugger).
And this XAML code:
<DataGrid AutoGenerateColumns="False" Name="gridOrders"
local:FilterableGrid.ObjectContext="{Binding Context}" local:FilterableGrid.IsFilterableGrid="True">
Where local is my namespace.
I user this code in IsFilterableGrid chaged event (atached behavior) to retrieve Object Context:
ObjectContext context = FilterableGrid.GetObjectContext(sourceGrid);
And context variable always ends up being null. Although DataContext for grid is not null and points to correct object (ViewModel).
Any ideas? I seem to be loosing my sanity on this...
Edit: I investigated a little more and if I bind local:FilterableGrid.IsFilterableGrid="{Binding IsFilterable}"
it works perfectly. But local:FilterableGrid.ObjectContext="{Binding Context}"
just doesnt! Maybe it has something to do with the type of value that gets passed (ObjectContext instead of string or say bool, which are primitive types?)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于解决了这个问题,甚至不确定有什么帮助 - 似乎这是 .NET 或 VS 中的一个错误。
我所做的是:向 RegisterAttached 调用添加一些属性元数据,并添加 OnChanged 委托。之后它就开始工作(正确绑定)。当我从 RegisterAttached 调用中删除属性元数据时,它继续工作。
所以我真的不知道那是什么,但现在它起作用了。
I finally did solve this one, nto even sure what did help - seems like it was kind of a bug in .NET or maybe VS.
What I did was: added some property metadata to RegisterAttached call, and added OnChanged delegate. After that it just started working (binding correctly). When I then removed property metadata from RegisterAttached call it continued working.
So I really do not know what it was, but now it works.