Search code examples
wpfxamlresourcesblend

Why are XAML resources unlike CSS styles?


In Expression Blend you can create a font-size of say 18 and then create a "font-size resource".

Coming at this from HTML/CSS, I cannot think of when I would want to make a style for a "font-size" and one for a "font-style" and one for a "font-weight". Instead I want to make a font called "CompanyHeader" and have 10 different attributes set in it, e.g. font-weight, font-style, font-size, color,etc.

Why is this different in Expression Blend, XAML, what is the sense of making a style/resource for each attribute?

this graphic shows how you can click on a little button on each attribute to change it into a resource: alt text http://tanguay.info/web/external/blendStyles.png


Solution

  • I have no experience with Blend, but styles in XAML can include more than one attribute, more then that, since unlike css you can only apply one style to an element you can't combine multiple one-attribute styles.

    Here is an example for a style that set multiple properties:

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
       <Page.Resources>
          <Style x:Key="MyStyle" TargetType="{x:Type Label}">
             <Setter Property="Width" Value="125"/>
             <Setter Property="Height" Value="25"/>
             <Setter Property="Background" Value="Red"/>
          </Style>
       </Page.Resources>
       <Label Style="{StaticResource MyStyle}"/>
    </Page>
    

    Note that if I wanted to break the style into 3 smaller styles each setting one property I couldn't use them because the Label's Style property can only accept one style.