Silverlight 2的Style练习

开发 后端
本文针对Silverlight 2的Style进行一个练习。Style和CSS的作用基本一样。

本篇的内容较为简单,主要针对Silverlight 2的Style进行一个练习。Style简要的说就是一些属性值的集合,
作用和CSS比较像。在Silverlight 2中定义Style只能同用于同种类型的Element。如下代码:

  1. < Style x:Key="ButtonStyleTwo" TargetType="Button"> 
  2.             < Setter Property="FontFamily" Value="Arial" /> 
  3.             < Setter Property="FontSize" Value="40" /> 
  4.             < Setter Property="Foreground" Value="Blue" /> 
  5.             < Setter Property="Background"> 
  6. < /Style> 


这就是一个定义好的Style,它只能用于Button组件,看看它如何产生作用,将Style直接嵌入Button:

  1. < Button Content="Button"> 
  2.     < Button.Style> 
  3.          < Style TargetType="Button"> 
  4.               < Setter Property="FontFamily" Value="Arial" /> 
  5.               < Setter Property="FontSize" Value="40" /> 
  6.               < Setter Property="Foreground" Value="Blue" /> 
  7.               < Setter Property="Background"> 
  8.                  < Setter.Value> 
  9.                    < LinearGradientBrush> 
  10.                      < GradientStop Color="Green" Offset="0">< /GradientStop> 
  11.                      < GradientStop Color="Red" Offset="1">< /GradientStop> 
  12.                    < /LinearGradientBrush> 
  13.                  < /Setter.Value> 
  14.               < /Setter> 
  15.          < /Style> 
  16.     < /Button.Style> 
  17. < /Button> 
  18.   

当然也可以通过Resource的方式来设置Style,分别设置了两个Style:ButtonStyleOne、ButtonStyleTwo,
ButtonStyleOne设置为默认Style,ButtonStyleTwo用于在点击Button后切换Style。

Silverlight 2的Style练习:XAML Code:

  1. < UserControl x:Class="SilverlightTest.Page" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     Width="400"> 
  5.     < UserControl.Resources> 
  6.         < Style x:Key="ButtonStyleOne" TargetType="Button"> 
  7.             < Setter Property="FontFamily" Value="Georgia" /> 
  8.             < Setter Property="FontSize" Value="40" /> 
  9.             < Setter Property="Foreground" Value="SlateGray" /> 
  10.             < Setter Property="Background"> 
  11.                 < Setter.Value> 
  12.                     < LinearGradientBrush> 
  13.                         < GradientStop Color="Blue" Offset="0">< /GradientStop> 
  14.                         < GradientStop Color="Yellow" Offset="1">< /GradientStop> 
  15.                     < /LinearGradientBrush> 
  16.                 < /Setter.Value> 
  17.             < /Setter> 
  18.         < /Style> 
  19.         < Style x:Key="ButtonStyleTwo" TargetType="Button"> 
  20.             < Setter Property="FontFamily" Value="Arial" /> 
  21.             < Setter Property="FontSize" Value="40" /> 
  22.             < Setter Property="Foreground" Value="Pink" /> 
  23.             < Setter Property="Background"> 
  24.                 < Setter.Value> 
  25.                     < LinearGradientBrush> 
  26.                         < GradientStop Color="Green" Offset="0">< /GradientStop> 
  27.                         < GradientStop Color="Red" Offset="1">< /GradientStop> 
  28.                     < /LinearGradientBrush> 
  29.                 < /Setter.Value> 
  30.             < /Setter> 
  31.         < /Style> 
  32.     < /UserControl.Resources> 
  33.     < StackPanel x:Name="LayoutRoot" Background="White"> 
  34.         < Button x:Name="TestButton" Content="A Customized Button"   
  35.                 Style="{StaticResource ButtonStyleOne}" Click="Button_Click">< /Button> 
  36.     < /StackPanel> 
  37. < /UserControl> 
  38.   

用C#来切换Sytle,如下代码:

  1. private void Button_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    //切换Style:ButtonStyleTwo  
  4.    TestButton.Style = this.Resources["ButtonStyleTwo"as Style;  
  5.    //修改Button文字  
  6.    TestButton.Content = "Style Changed";  
  7. }  

以上就是Silverlight 2的Style练习。

【编辑推荐】

  1. 微软Silverlight 3挑战Adobe AIR
  2. RIA大战在即 微软Silverlight胜算几何
  3. 图解Silverlight 3的7个新功能
  4. Flash与Silverlight多领域实测对比
  5. 微软宣称Silverlight装机量超过三亿
责任编辑:yangsai 来源: 博客园
相关推荐

2012-07-05 15:22:18

Silverlight

2009-12-31 17:00:40

Silverlight

2009-02-02 10:53:34

SilverlightSilverlightRIA

2009-02-02 10:08:25

SilverlightTwitterASP.NET

2010-01-04 16:30:06

Silverlight

2011-07-15 14:12:30

Silverlight

2009-05-26 11:33:15

SilverlightIE2微软

2009-03-05 11:40:31

ListBox开发Silverlight

2010-08-09 13:25:02

FlexSilverlight

2010-08-24 13:55:38

DIVstyle

2011-04-18 13:52:31

Ubuntu MAC

2017-02-06 13:31:19

Android样式继承关系

2009-06-03 10:24:11

LoadMaskSilverlight

2009-02-20 08:54:20

DownloaderSilverlight对象

2011-09-30 08:55:12

Silverlight

2009-06-25 09:00:43

Silverlight

2009-07-16 14:58:16

Windows Emb

2011-01-27 08:49:47

SilverlightWeb.NET

2011-01-26 09:50:18

Silverlight.NetJavaScript

2012-12-24 13:27:55

微软Style公有云Windows Azu
点赞
收藏

51CTO技术栈公众号