Silverlight Style应用想过技巧分享

开发 开发工具
Silverlight Style应用对于初学者来说是一个比较重要的操作步骤。我们在这篇文章中就为大家详细介绍相关操作方法。

Silverlight工具的应用方法多种多样。如果想要全部掌握浙西而应用方法是一个比较复杂的过称。我们可以在实践总去逐渐的积累这些应用技巧。本篇的内容较为简单,主要针对Silverlight Style应用进行一个练习。Style简要的说就是一些属性值的集合,作用和CSS比较像。在Silverlight2中定义Style只能同用于同种类型的Element。如下代码:#t#

  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> 

这就是一个定义好的Silverlight Style应用,它只能用于Button组件,看看它如何产生作用,将Style直接嵌入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>  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

 

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

 

XAML Code:  
 
< UserControl x:Class="Silverligh
tTest.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>  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.

 

用C#来切换Silverlight Style应用,如下代码:

 

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

 

责任编辑:曹凯 来源: 博客园
相关推荐

2009-12-29 17:56:47

Silverlight

2009-12-30 18:23:13

Silverlight

2010-01-04 14:35:55

Silverlight

2009-12-29 16:08:41

Silverlight

2009-12-30 10:25:03

Silverlight

2009-12-30 13:37:24

Silverlight

2010-01-04 14:14:43

Silverlight

2009-12-30 16:19:49

Silverlight

2009-12-30 18:18:32

Silverlight

2009-08-03 18:46:38

Silverlight

2012-07-05 15:22:18

Silverlight

2009-12-15 10:23:23

Ruby应用技巧

2009-12-31 10:21:53

Silverlight

2009-12-31 16:44:53

Silverlight

2009-12-31 11:15:57

Silverlight

2010-01-25 11:09:58

Android Htt

2009-12-30 09:55:51

Silverlight

2010-02-01 11:13:00

C++ Traits

2009-12-18 10:47:16

Ruby装饰模式

2010-01-25 16:08:37

Android ADB
点赞
收藏

51CTO技术栈公众号