Silverlight中样式使用方式

简介:

本来不打算写这篇博,其实关于Silverlight中样式很简单但 又感觉这里面东西虽少但很琐碎,要用到地方还需要在回头看.

Silverlight中通过使用样式来控制控件感官效果,本质上和HTML元素定义样式有些类似. 如果进行分类的话可以分成:

A:内置样式

B:全局样式

其实在Silverlight对样式控制这块没有具体的分什么内置和全局,主要是为了区分样式的使用方式 才这么叫方便用户的理解

A:内置样式

所谓的内置就是对单个XAML中元素直接在标签中定义样式属性,内置样式缺点在于它不可重用,额外的代码使的XAML文件页面代码容易和主题业务代码混合,照成一定混乱,不推荐使用这种方式.

 
  1. --内置样式  
  2.  <Button Content="测试Button" Canvas.Left="30" Canvas.Top="120" Height="25"   
  3.             Foreground="Black" 
  4.             Background="Azure" 
  5.             FontSize="14" 
  6.             >  
  7.               
  8.             </Button>  
  9.   //从上面可以看出直接在标签中定义 

B:全局样式

全局正好解决这个问题,可以使定义的样式重用,而且是XAML文件页面代码更加简洁,只需用户专注于业务

 
  1. --在App.xaml文件中<ApplicationResource>节点中定义共用样式  
  2. --定义样式标签<Style>    
  3.    <Application.Resources>  
  4.         <!--add the Application Resources-->  
  5.         <!--Define the CSS Style About WebControllers-->  
  6.         <Style x:Key="firstSty" TargetType="Button">  
  7.             <Setter Property="FontSize" Value="15"></Setter>  
  8.             <Setter Property="FontFamily" Value="微软雅黑"></Setter>  
  9.             <Setter Property="Foreground" Value="Red"></Setter>  
  10.             <Setter Property="Background" Value="Silver"></Setter>  
  11.         </Style>  
  12.           
  13.         <Style x:Key="secondSty" TargetType="Button">  
  14.             <Setter Property="FontFamily" Value="微软雅黑"></Setter>  
  15.             <Setter Property="FontSize" Value="15"></Setter>  
  16.             <Setter Property="Foreground" Value="Black"></Setter>  
  17.             <Setter Property="Background" Value="blue"></Setter>  
  18.         </Style>  
  19.           
  20.     </Application.Resources>  
  21.  
  22. --<Style>标签中X:key主要用来唯一表示样式  TargetType:指定样式作用的目标控件是什么类型  
  23. --子节点<Setter> 通过Property和Value来设置具体的样式属性和值  

在XAML页面中引用:

 
  1. <!--测试定义在App.xaml文件中样式定义-->  
  2.             <Button Content="firsttext" Canvas.Left="30" Canvas.Top="120" Height="25"   
  3.              Foreground="Black" 
  4.              Background="Azure" 
  5.              FontSize="14" 
  6.              Style="{StaticResource firstSty}" 
  7.             >  
  8.               
  9.             </Button>  
  10.    --通过标签中Style属性StaticResource标记句法来指定具体的样式 

注意问题:样式定义冲突

 

 
  1. --这是在App.XAML中定义的样式  
  2. <Application.Resources>  
  3.         <!--add the Application Resources-->  
  4.         <!--Define the CSS Style About WebControllers-->  
  5.         <Style x:Key="firstSty" TargetType="Button">  
  6.             <Setter Property="FontSize" Value="15"></Setter>  
  7.             <Setter Property="FontFamily" Value="微软雅黑"></Setter>  
  8.             <Setter Property="Foreground" Value="Red"></Setter>  
  9.             <Setter Property="Background" Value="Silver"></Setter>  
  10.         </Style>  
  11.  
  12.  </Application.Resources>  
  13.  
  14. --XAML页面中引用  
  15. <Button Content="firsttext" Canvas.Left="30" Canvas.Top="120" Height="25"   
  16.             Background="Azure" 
  17.             FontSize="14" 
  18.             Style="{StaticResource firstSty}" 
  19.             >  
  20.  
  21. //在页面引用中有再次定义了BackGround和FontSize属性,这时标签会从就近原则来样式,就是以先取标签内的属性为主,然  
  22. //后在来查看Style中的对外引用  
  23.  
  24. 那么这样BackGround显示为Azure 而非App.XAML中定义的Silver(银灰色) 

 

本文转自chenkaiunion 51CTO博客,原文链接:http://blog.51cto.com/chenkai/765428


相关文章
|
C#
艾伟:Silverlight 里如何实现隐式样式,ImplicitStyleManager 的实现思想
在 WPF 中,我们可以方便的在全局范围定义一个样式,就可以应用到所有这种类型的对象,这就是所谓的隐式样式(implicit Style),比如: WPF中定义样式 Button aButton b 这样之后,两个按钮就都变成了浅蓝色的背景。
785 0
|
C# 前端开发
WPF,SilverLight中直线的样式示例
原文:WPF,SilverLight中直线的样式示例 XAML代码:// LineStyle.xaml                                                           ...
697 0

热门文章

最新文章