Вопрос

All -

I am trying to understand when I would use the attached behavior pattern (creating a static class and registering DP properties through RegisterAttached) versus Property and/or Data Triggers in a datagrid. Looking for common use-cases of both specially in a WPF datagrid scenario

My understanding is that when I want to do something with data (in code behind and keeping MVVM intact) - I would be using Attached behavior pattern (where the default command is not available and hence extends the functionality like double click, multi-row select etc similar to Extension methods) whereas I would use triggers in combination with Styles/Templates when I want to set something visually on the grid (like a certain value would flash a certain color on the cell etc)

I can think of one - if I need to multiselect 3 rows in datagrid and then click the delete button - assuming that is a perfect candidate for Attached Behavior.

Please note - I am not asking the definitions of Attached Properties and/or Triggers. I very well understand them. Looking more for guidance on real world usage.

Can the experts plz provide some insight

Thanks

Это было полезно?

Решение

You're on the right track, but attached behaviors can also be used purely for visual effects (thus having no effect on the ViewModel).

I think it comes down to what is needed for the situation. For example, in my current application, Attached Behaviors are preferable because we target both WPF and Silverlight (and triggers don't exist in SL).

If there is a trigger that does what you need, go for it, but if you need to extend the behavior then Attached Properties are a great way to do so.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top