I wrote a blog post some time ago, where I took the CSS selector mechanism (Fizzler) and applied it to WPF:
http://www.scottlogic.co.uk/blog/colin/2009/03/using-css-selectors-for-styling-in-wpf/
It allows you to use the CSS selector for target elements and apply styles to them. It also combines styles so that if multiple CSS selectors match, the styles from each are combined together.
See the following example:
<css:StyleSheet x:Key="cssStyles"> <css:StyleSheet.Rules> <css:StyleRule Selector=".form Grid *" SelectorType="LogicalTree"> <css:StyleRule.DeclarationBlock> <css:StyleDeclarationBlock> <css:StyleDeclaration Property="Margin" Value="4,4,4,4"/> </css:StyleDeclarationBlock> </css:StyleRule.DeclarationBlock> </css:StyleRule> <css:StyleRule Selector=".form TextBlock.mandatory"> <css:StyleRule.DeclarationBlock> <css:StyleDeclarationBlock> <css:StyleDeclaration Property="Foreground" Value="Red"/> </css:StyleDeclarationBlock> </css:StyleRule.DeclarationBlock> </css:StyleRule> <css:StyleRule Selector="Border.form"> <css:StyleRule.DeclarationBlock> <css:StyleDeclarationBlock> <css:StyleDeclaration Property="BorderThickness" Value="2"/> <css:StyleDeclaration Property="BorderBrush" Value="Black"/> <css:StyleDeclaration Property="CornerRadius" Value="5"/> <css:StyleDeclaration Property="Margin" Value="10,10,10,10"/> </css:StyleDeclarationBlock> </css:StyleRule.DeclarationBlock> </css:StyleRule> <css:StyleRule Selector=".form .title"> <css:StyleRule.DeclarationBlock> <css:StyleDeclarationBlock> <css:StyleDeclaration Property="HorizontalAlignment" Value="Stretch"/> <css:StyleDeclaration Property="HorizontalContentAlignment" Value="Center"/> <css:StyleDeclaration Property="Background" Value="DarkBlue"/> <css:StyleDeclaration Property="Foreground" Value="White"/> <css:StyleDeclaration Property="FontSize" Value="13"/> <css:StyleDeclaration Property="Padding" Value="3,3,3,3"/> <css:StyleDeclaration Property="FontWeight" Value="Bold"/> </css:StyleDeclarationBlock> </css:StyleRule.DeclarationBlock> </css:StyleRule> </css:StyleSheet.Rules> </css:StyleSheet>
source share