Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Style in Silverlight 2

Shulou Source: shulou.com Published: 2022-06-02 08:32:50 10月03日 Update

Editor to share with you how to use Style in Silverlight 2, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Style is simply a collection of attribute values.

The effect is similar to CSS. Defining Style in Silverlight 2 can only be used for the same type of Element. The code is as follows:

< Style x:Key="ButtonStyleTwo" TargetType="Button">

< Setter Property="FontFamily" Value="Arial" />

< Setter Property="FontSize" Value="40" />

< Setter Property="Foreground" Value="Blue" />

< Setter Property="Background">

< /Style>

This is a defined Style that can only be used with Button components to see how it works, embedding Style directly into Button:

< Button Content="Button">

< Button.Style>

< Style TargetType="Button">

< Setter Property="FontFamily" Value="Arial" />

< Setter Property="FontSize" Value="40" />

< Setter Property="Foreground" Value="Blue" />

< Setter Property="Background">

< Setter.Value>

< LinearGradientBrush>

< GradientStop Color="Green" Offset="0">

< /GradientStop>

< GradientStop Color="Red" Offset="1">

< /GradientStop>

< /LinearGradientBrush>

< /Setter.Value>

< /Setter>

< /Style>

< /Button.Style>

< /Button>

Of course, you can also set Style by Resource, setting two Style:ButtonStyleOne and ButtonStyleTwo respectively

ButtonStyleOne is set to the default Style,ButtonStyleTwo to switch Style after clicking Button.

Style activity for Silverlight 2: XAML Code:

< UserControl x:Class="SilverlightTest.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400">

< UserControl.Resources>

< Style x:Key="ButtonStyleOne" TargetType="Button">

< Setter Property="FontFamily" Value="Georgia" />

< Setter Property="FontSize" Value="40" />

< Setter Property="Foreground" Value="SlateGray" />

< Setter Property="Background">

< Setter.Value>

< LinearGradientBrush>

< GradientStop Color="Blue" Offset="0">

< /GradientStop>

< GradientStop Color="Yellow" Offset="1">

< /GradientStop>

< /LinearGradientBrush>

< /Setter.Value>

< /Setter>

< /Style>

< Style x:Key="ButtonStyleTwo" TargetType="Button">

< Setter Property="FontFamily" Value="Arial" />

< Setter Property="FontSize" Value="40" />

< Setter Property="Foreground" Value="Pink" />

< Setter Property="Background">

< Setter.Value>

< LinearGradientBrush>

< GradientStop Color="Green" Offset="0">

< /GradientStop>

< GradientStop Color="Red" Offset="1">

< /GradientStop>

< /LinearGradientBrush>

< /Setter.Value>

< /Setter>

< /Style>

< /UserControl.Resources>

< StackPanel x:Name="LayoutRoot" Background="White">

< Button x:Name="TestButton" Content="A Customized Button" Style="{StaticResource ButtonStyleOne}" Click="Button_Click">

< /Button>

< /StackPanel>

< /UserControl>

Use C # to switch Sytle, as follows:

Private void Button_Click (object sender, RoutedEventArgs e) {/ / switch Style:ButtonStyleTwo TestButton.Style = this.Resources ["ButtonStyleTwo"] as Style; / / modify the Button text TestButton.Content = "Style Changed";} above is all the content of this article "how to use Style in Silverlight 2". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Articles switching code function content that is not much two can be passed most attributes text methods more knowledge summary types components industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info OPPO Reno Microsoft macOS