tags:

views:

72

answers:

2

Hi,

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.

+3  A: 

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.

JaredPar
+2  A: 

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.

Reed Copsey
@Reed. Referring to point 1, I removed the GetXXX and SetXXX methods and I get an XAMLParserException saying the Attached property does not exist in XML name space. So it appears that they have to exist?
JD
@JD: You're right - they need to be there for the property system to work in the designer. This is mentioned here: http://msdn.microsoft.com/en-us/library/ms749011.aspx#custom I edited to remove that. However, the meta data callback is the correct way to track changes in the property, as XAML (and the binding system in general) will never call the Set/Get methods.
Reed Copsey
@Reed. Thanks, seems to make sense after your explanation ad JaredPar.
JD