futurice / windows-app-development-best-practices

A collection of best practices for Windows App and C# developers
Other
363 stars 45 forks source link

Attached Properties #4

Open jarnmo opened 9 years ago

jarnmo commented 9 years ago

Feel free to comment and discuss Attached Properties here.

What are they good or bad for? Good practices in using them? What kind of issue have you run into? What are the alternatives?

Any thoughts are welcome!

jarnmo commented 9 years ago

Personally, I've found Attached Properties to be a very handy way to add functionality into controls, especially the ones I'm not in control of. The properties are a bit tedious to create, but very easy to use and I haven't really run into any surprises with them.

I guess the kind of functionality you can implement with Attached Properties is the same as what you can do with Behaviors. With both you can only access the public API of the control in question. However, that is enough for a lot of cases. I'm pretty sure you can't set either in a Style property Setter, at least in WinRT 8.1. You can obviously use both in a data or a control template.

Using behaviors requires you to type a bit more XAML, but they are quite nicely supported by Expression Blend. I'm not sure if AttachedProperties are supported by the designers at all. Also, I haven't measured, but would be interested to hear how they compare on runtime performance.