Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 951
Edward Tanguay Avatar asked Dec 03 '22 08:12

Edward Tanguay


2 Answers

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.

like image 92
Nir Avatar answered Dec 18 '22 10:12

Nir


I think they likely allow for the creation of single resources for FontFamily, FontWeight, etc. to allow them to be used across many styles in the application. By placing a single property in a resource you can effect all styles using that resource at once. If you weren't using a resource but were attempting to use a consistent FontFamily across your whole application (or a portion of it) then you had to go through each style one at a time in order to update it.

In order to make a style with multiple properties in blend you can do the following:

  • Select the control you wish to style (the control's type will be used as the TargetType for the style)
  • From the menu select Object->Edit Style->Create Empty
  • Enter the Key you would like to assign to the style (this is the name that you will use to reference the style)
  • Go to the properties tab and begin to apply the look/feel that you want for that style
like image 29
Richard McGuire Avatar answered Dec 18 '22 09:12

Richard McGuire