I have set break points on my attached properties SetXXX and GetXXX static methods. In Xaml, I have assigned values to the attached property. However, I was expecting the Set or Get static methods to be called but they are not. The attached property works as expected and if I call SetXXX and GetXXX methods in code, then it works are expected.
Why are the methods not called when set from Xaml?
JD.
An attached property is intended to be used as a type of global property that is settable on any object. In Windows Presentation Foundation (WPF), attached properties are typically defined as a specialized form of dependency property that does not have the conventional property wrapper”.
Attached properties are properties which can be set on any wpf object (basically, at least a DependencyObject) via the DependencyObject. SetValue method. The purpose for this mechanism is to "attach" to other objects information needed by parent objects, not the child objects themselves. For example, the Grid.
XAML does not understand normal property getter and setters when it comes to attached properties. Instead it goes straight to the lower level APIs on DependencyObject
. Namely the GetValue
and SetValue
methods.
As JaredPar explained, when you use XAML, the GetXXX/SetXXX methods are not called.
I wanted to add something, though:
If you need to track changes to the Attached Property in code, you should use the Metadata. You can set a callback in the metadata to fire when the property changes, and track it in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With