Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why attached property Set and Get static methods are not called in XAML?

Tags:

.net

wpf

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.

like image 618
JD. Avatar asked Apr 02 '10 19:04

JD.


People also ask

What is attached property and dependency property in WPF?

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”.

What is attached property in WPF with example?

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.


2 Answers

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.

like image 67
JaredPar Avatar answered Nov 15 '22 08:11

JaredPar


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.

like image 43
Reed Copsey Avatar answered Nov 15 '22 06:11

Reed Copsey