Silverlight 2b1 Styles

Setting default control styles (and templates) in Silverlight 2.0 Beta 1 works a bit differently than WPF. In WPF, I can set a style in an element's resource dictionary (i.e. Application.Resources or Window.Resources) that will automatically be applied to all descendents.

Say I wanted to change the text color of all TextBlocks in my application, I could create this style in App.xaml:

<Style TargetType="TextBlock">
    <Setter Property="Foreground" Value="Blue" />
</Style>

And the the following TextBlock in Window1.xaml would automatically have its Foreground property set to blue.

<TextBlock >Hai World!</TextBlock>

However, this technique does not work in Silverlight 2.0 Beta 1. In Silverlight, you must explicitly set the style's key, and then bind all TextBlocks in your application to the style.

In your App.xaml:

<Style x:Key="textBlockStyle" TargetType="TextBlock">
    <Setter Property="Foreground" Value="Yellow" />
</Style>

and in your Page1.xaml:

<TextBlock Style="{StaticResource textBlockStyle}">Hai World!</TextBlock>

If you are planning on theming or skinning your controls at all, you should define all your styles up front, declare them in your App.xaml and bind the style in every instance of a control. Kind of cumbersome compared to WPF's default styling behavior.

This applies even if you make use of UserControls in a library assembly.

<UserControl ...>
    <StackPanel x:Name="LayoutRoot" Background="White">
        <TextBlock Style="{StaticResource textBlockStyle}">
            I is in ur CPU
        </TextBlock>
        <Button Height="25" Width="75" Content="Click Me!" />
    </StackPanel>
</UserControl>

The referenced textBlockStyle does not exist in the library assembly, but when loaded by the application, it does exist and all is good. Interestingly, Visual Studio's designer seems to get a bit confused by this, but Expression Blend 2.5 Preview handles the "remote" style just fine. That is, when I look at MyTextBlock.xaml, the designer displays the control with the application's style applied.

Download the sample solution file to see this in action.

Published April 17, 2008

AttachmentSize
StylesAndTemplates.zip157.6 KB

txKYGxFKsSjSDkRhoab

4KLBdz wdtkyuzzdpwp, [url=http://tplucpvunwqt.com/]tplucpvunwqt[/url], [link=http://dptltkqarjee.com/]dptltkqarjee[/link], http://uichkxlrteaq.com/

LwzckmkiNJbJm

fCc9AM ijfwhfhapscd, [url=http://yfuffzxtwbxw.com/]yfuffzxtwbxw[/url], [link=http://yaajuwmjjkzi.com/]yaajuwmjjkzi[/link], http://xronhivyispr.com/

nGZBnihEYDzbgmzJsPM

Y9nJkj efxjegdtrgaz, [url=http://dvllqpjfskra.com/]dvllqpjfskra[/url], [link=http://wbpnnwnxqpzo.com/]wbpnnwnxqpzo[/link], http://bcgqrcxicbow.com/

GHlWxHLBCLR

uZEdka uixjcrxjyhhw, [url=http://ppevwhiewpio.com/]ppevwhiewpio[/url], [link=http://xguhwknpybfs.com/]xguhwknpybfs[/link], http://knyepjmoqwhc.com/

KEQPCYWXZTh

7uVXWM gjxjiwiyvmfe, [url=http://ghfduzcvcqhg.com/]ghfduzcvcqhg[/url], [link=http://sxejxctnetqg.com/]sxejxctnetqg[/link], http://ghfujndzqoij.com/

paWQqJyrEUdJApveKg

comment6, rocaltrol, putcnm, tulasi, 8113, buy high love online, %-((, propecia, vir, buy cipro, :-DDD, buy danazol, 665231, buy nirdosh online, 193, buy plendil, %-DD, buy brafix online, 6214,