开发者

Cannot bind ObjectContext from DataContext to custom Attached Property

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:Filter开发者_C百科ableGrid.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?)?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜