Styling in Silverlight is similar to styling in HTML with CSS. It helps you keep your markup (HTML, XAML) code clean by putting all the properties into a separate file. At runtime, both the markup and the styles defined in the external style sheet are merged and give the look to the application as it was intended.
The following piece of XAML code actually creates a default style for the TextBlock type and a named style called SectionHeaderStyle.
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="14"></Setter>
<Setter Property="FontWeight" Value="Normal"></Setter>
<Setter Property="FontFamily" Value="Verdana"></Setter>
<Setter Property="FontStyle" Value="Normal"></Setter>
</Style>
<Style x:Key="Header" TargetType="TextBlock">
<Setter Property="FontSize" Value="20"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="FontFamily" Value="Verdana"></Setter>
<Setter Property="FontStyle" Value="Italic"></Setter>
</Style>
<Setter Property="FontSize" Value="14"></Setter>
<Setter Property="FontWeight" Value="Normal"></Setter>
<Setter Property="FontFamily" Value="Verdana"></Setter>
<Setter Property="FontStyle" Value="Normal"></Setter>
</Style>
<Style x:Key="Header" TargetType="TextBlock">
<Setter Property="FontSize" Value="20"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="FontFamily" Value="Verdana"></Setter>
<Setter Property="FontStyle" Value="Italic"></Setter>
</Style>
On the TextBlocks that should be using this style, we do not need to add a Style attribute:
<StackPanel>
<TextBlock x:Name="SpecficStyle" Text="TextBlock with a Style" Style="{StaticResource Header}"></TextBlock>
<TextBlock x:Name="DefaultStyle" Text="TextBlock with a Default Style"></TextBlock>
<TextBlock x:Name="SpecficStyle" Text="TextBlock with a Style" Style="{StaticResource Header}"></TextBlock>
<TextBlock x:Name="DefaultStyle" Text="TextBlock with a Default Style"></TextBlock>
</StackPanel>
0 comments:
Post a Comment