5

Possible Duplicate:
WPF: XAML property declarations not being set via Setters?

I am stuck on seemingly silly problem.

I have a user control, MyControl.xaml, and MyControl.xaml.cs defines a public dependency property:

public static readonly DependencyProperty VisibleItemsProperty =
    DependencyProperty.Register("VisibleItems", typeof(object), typeof(MyControl));

public object VisibleItems
{
    get { return (object)GetValue(VisibleItemsProperty); }
    set { SetValue(VisibleItemsProperty, value); }
}

Within another view SomeOtherViewA, i declare my control:

<cc:MyControl VisibleItems="{Binding VisibleTables}"  />

VisibleTables is a dependency property on the viewmodel SomeOtherViewModelA.

I know that VisibleTables returns values, because it is bound to other controls within SomeOtherViewA (such as ListBox) and they work fine.

For some reason the dependency property within my custom user control is never set. Am i missing something obvious?

Community
  • 1
  • 1
Sonic Soul
  • 23,855
  • 37
  • 130
  • 196
  • I noticed this behavior as well and just assumed that this has to do with how XAML is evaluated by the WPF engine & mechanics of DPs, that is why i never mess with the setters of DPs anymore. Interesting question, wonder how exactly that works. – H.B. Apr 13 '11 at 03:37
  • Did you mean in your Dependency Property typeof(MyControl) instead of typeof(SpyFilterList) – aqwert Apr 13 '11 at 03:38
  • yes, I changed the names to simplify my question. adjusted my post, thanks! – Sonic Soul Apr 13 '11 at 03:40

1 Answers1

4

I think i just found a duplicate.

Also more about this on MSDN:

The WPF XAML processor uses property system methods for dependency properties when loading binary XAML and processing attributes that are dependency properties. This effectively bypasses the property wrappers. When you implement custom dependency properties, you must account for this behavior and should avoid placing any other code in your property wrapper other than the property system methods GetValue and SetValue.

(SetValue is being called directly, those wrapper properties are just there for convenience in code behind)

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Interesting. Not very intuitive.. Must wrap brain around new reality! – Sonic Soul Apr 13 '11 at 03:59
  • yeah, what's really fascinating is that WPF doesn't actually call your setter/getter but it will still fail to compile if you leave them out. – Robert Levy Apr 13 '11 at 04:03
  • @Robert Levy: Are you sure that it is not your context's dependency on the wrapper that stops it from compiling? I just tried defining a lone DP and it worked fine, could bind to it and change its value using the `SetValue` method just fine. – H.B. Apr 13 '11 at 04:15
  • Figure that the XAML parser is really assigning an instance of Binding to your VisibleItems property. As VisibleItems is an object, not Binding, the GetValue/SetValue methods allow the DependencyObject to support values of either object or Binding. – Ed Chapel Apr 13 '11 at 04:16