Silverlight 4’s New Implicit Styles

Silverlight 4 introduces a boatload of big-ticket features such as printing support, webcam and microphone support, and support for elevated trust in out-of-browser applications. It’s also chock full of minor improvements that fly lower under the radar. One of my favorite features in this category is support for implicit styles.

In Silverlight 3, there was no built-in way to define a style and have it automatically applied to all controls of a specified type. Sure, you could use the Silverlight Toolkit’s implicit style manager to apply default styles, but the fact remained that the run-time itself lacked support for the same.

In Silverlight 4, you can define an implicit style by omitting the x:Key attribute from the <Style> element. Unless otherwise specified, that style will be applied to all elements of the type specified by the style’s TargetType. Here’s an example. The following XAML declares three buttons and styles them with an implicit style that rotates them 15 degrees:

<UserControl.Resources>

    <Style TargetType=”Button”>

        <Setter Property=”RenderTransformOrigin” Value=”0.5,0.5″ />

        <Setter Property=”RenderTransform”>

            <Setter.Value>

                <RotateTransform Angle=”15″ />

            </Setter.Value>

        </Setter>

    </Style>

</UserControl.Resources>

<Grid x:Name=”LayoutRoot” Background=”White”>

    <StackPanel Orientation=”Horizontal” HorizontalAlignment=”Center”>

        <Button Width=”120″ Height=”60″ Content=”Button1″ Margin=”8″ />

        <Button Width=”120″ Height=”60″ Content=”Button2″ Margin=”8″ />

        <Button Width=”120″ Height=”60″ Content=”Button3″ Margin=”8″ />

    </StackPanel>

</Grid>

Here’s the result:

Implicit Styles (1) 

Of course, you might want to use implicit styles but exclude certain controls from being implicitly styled. You can do that by setting a control’s Style property to x:Null:

<UserControl.Resources>

    <Style TargetType=”Button”>

        <Setter Property=”RenderTransformOrigin” Value=”0.5,0.5″ />

        <Setter Property=”RenderTransform”>

            <Setter.Value>

                <RotateTransform Angle=”15″ />

            </Setter.Value>

        </Setter>

    </Style>

</UserControl.Resources>

<Grid x:Name=”LayoutRoot” Background=”White”>

    <StackPanel Orientation=”Horizontal” HorizontalAlignment=”Center”>

        <Button Width=”120″ Height=”60″ Content=”Button1″ Margin=”8″ />

        <Button Width=”120″ Height=”60″ Content=”Button2″ Margin=”8″ />

        <Button Width=”120″ Height=”60″ Content=”Button3″ Margin=”8″ Style=”{x:Null}” />

    </StackPanel>

</Grid>

And here is the resulting UI:

Implicit Styles (2)

Implicit styles are a welcome addition to Silverlight 4, and I’m confident that developers will find this feature to be as useful in Silverlight as they have already found it to be in WPF.

Stay Informed

Sign up for the latest blogs, events, and insights.

We deliver solutions that accelerate the value of Azure.
Ready to experience the full power of Microsoft Azure?

Atmosera is thrilled to announce that we have been named GitHub AI Partner of the Year.

X