#137 – The Value of the Style Property Comes From One of Three Places

The Style property is the one dependency property that doesn’t follow the full list of property value precedence rules.  Its value can come from one of three different sources, listed below.

  1. Explicit style – Style is explicitly specified, either inline in XAML, as a resource, or specified in code.  Treated as a local value
  2. Implicit style – Style is applied to all elements whose type matches the target type of the style
  3. Default – No style is specified, default styling used

Here’s an example of an explicit style.

    <Window.Resources>
        <Style x:Key="anItalicButton">
            <Setter Property="Control.FontStyle" Value="Italic"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Content="Click Me" Height="23" Width="125" Style="{StaticResource anItalicButton}"/>
        <Button Content="Or Me" Height="23" Width="125" />
    </StackPanel>

Here’s an example of an implicit style, applied to all Button elements.

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Control.FontStyle" Value="Italic"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Content="Click Me" Height="23" Width="125" />
        <Button Content="Or Me" Height="23" Width="125" />
    </StackPanel>

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: