views:

34

answers:

2

When writing WPF/Silverlight applications, I sometimes struggle when determining whether or not a property should go inside a style or be left directly on the element. What guidelines do you use when deciding?

+3  A: 

I think it generally comes down to style reuse and overall project organization.

If it is a style you are going to be reusing across many different elements in a control (such as Brushes) or even in many different controls in your project you are obviously going to want to pull it out into its own resource. For better organization you probably even going to want to put these shared styles in their own resource dictionary file and used MergedDictionaries to pull them in to different controls. You can actually create some pretty useful cascading effects between different styles using the BasedOn attribute as well.

When it comes down just a "one-off" feature of a specific element I think its fine to keep style attributes directly on the element. If it doesn't need to be shared and the style is unique to the one element (specific Border, Margin, etc) it is much easier and more readable to keep it directly on the element.

Dan Auclair
+1  A: 

Very good question. There are two situations where I move a property into a style.

1) Over Crowded Properties - Over crowded properties are nothing but, too many properties for customizing an element. Say for example. If you have a text block and want to customize almost all the properties. Then, it'll reduce the readability. That too if it has somethings like triggers. It'll be too much crowded. By that time, I used to move them to a style.

2) Repeatedly Used Styles - This is obvious case, if I want to apply a same set of properties to be applied for a control. Usually we do this by x:key or TargetType.

HTH

Avatar