SilverLigth学习笔记--控制 Silverlight控件样式

1、内联方式--即直接在控件内部利用其属性进行设置

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<UserControl x:Class="RemoveTextBoxBorder.Page"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width
="400" Height="300">
    
<Grid x:Name="LayoutRoot" Background="White">
        
<Button Content="Button" Height="75" Width="125" BorderBrush="Green" Foreground="Blue" />
    
</Grid>
</UserControl>

2、全局方式--在 App.xaml Resources 文件中进行定义
当你在VS2008中创建Silverlight项目中,你会得到一个名叫 "App.xaml"的文件,此文件格式如下:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class
="RemoveTextBoxBorder.App"
             
>
    
<Application.Resources>
    
</Application.Resources>
</Application>

 在此文件中你可以定义自己的样式,定义样式采用如下格式:

 

 

  < Style x:Key = " 样式名 "  TargetType = " 样式所针对的控件类型 " >  
   
< Setter Property = " 控件属性名 "  Value = " 控件属性值 "   />
 
</ Style >

 在本示例中我们在此文件加入如下样式定义,加入后的App.xaml文件内容如下

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class
="RemoveTextBoxBorder.App"
             
>
    
<Application.Resources>
        
<Style x:Key="ButtonStyleOne" TargetType="Button">
            
<Setter Property="BorderBrush" Value="Green" />
            
<Setter Property="Foreground" Value="Blue" />
            
<Setter Property="BorderThickness" Value="4,4,4,4" />
        
</Style>
        
<Style x:Key="ButtonStyleTwo" TargetType="Button">
            
<Setter Property="BorderBrush" Value="Blue" />
            
<Setter Property="Foreground" Value="Green" />
        
</Style>
    
</Application.Resources>
</Application>

然后我们在控件的XAML文件中引用所定义的样式

 

ContractedBlock.gif ExpandedBlockStart.gif Code
<UserControl x:Class="RemoveTextBoxBorder.Page"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width
="400" Height="300">
    
<Grid x:Name="LayoutRoot" Background="White">
        
<Button Content="Button" Height="75" Width="125" Style="{StaticResource ButtonStyleOne}"  />        
    
</Grid>
</UserControl>

 

3、在 C#代码中动态设置控件样式
 为了在代码中引用此控件,我们必须要为此控件命名,在此我们命名为"MyButton"

 

< Button x:Name = " MyButton "  Content = " Button "  Height = " 75 "  Width = " 125 "   />      

然后在Page.xaml的code-behind 文件中,在其构造函数中加入如下代码:
   MyButton.Style = Application.Current.Resources["ButtonStyle"] as Style;
 后台代码如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
     public partial class Page : UserControl
    {
        
public Page()
        {
            InitializeComponent();
            MyButton.Style 
= Application.Current.Resources["ButtonStyleOne"as Style;
        }
    }

 

 Silverlight学习笔记清单

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值