Styles

Developers of business applications might be a bit wary of becoming an expert in styling. Windows Forms had no such capability, and web applications often have specialists that do the styling.

However, styles in XAML are quite a bit more flexible than styles in earlier technologies, and address functionality that's not strictly cosmetic. That means developers need to understand styles, too.

What Is a Style?

At the most basic level, a Style in XAML is simply a way to set several property values for an element at one time. In syntactical terms, a Style is a collection of Setter objects, and each Setter object knows how to set a particular property to a particular value.

Here is a simple Style for a Button element (code file: SimpleStyle.xaml):

<Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="Margin" Value="4" />
    <Setter Property="FontSize" Value="22"/>
    <Setter Property="FontStyle" Value="Italic" />
    <Setter Property="Foreground" >
        <Setter.Value>
            <LinearGradientBrush
                EndPoint="0.8,0.8" 
                StartPoint="0.2,0.2">
                <GradientStop Color="DarkBlue" Offset="0"/>
                <GradientStop Color="ForestGreen" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
</Style>

In this case, the Style is declared as a resource, and so has an x:Key attribute. That's the common case. There are a few scenarios where a Style is not used as a resource, and in fact you will see one a bit further on. However, normally the power of a Style is to apply it widely through much or all of an application, ...

Get Professional Visual Basic 2012 and .NET 4.5 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.