SilverLight样式表
by 松原蔡晓冬
at 2010-07-02 19:10:00
original http://www.cnblogs.com/caixiaodong/archive/2010/07/02/1770183.html
作者: 松原蔡晓冬 发表于 2010-07-02 19:10 原文链接 阅读: 764 评论: 3
SilverLight样式表分为全局样式表、局部样式表、内部样式表。样式支持向下继承。每个都具体举个简单例程。
1 全局样式表
在App.Xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication2.App"
>
<Application.Resources>
<Style x:Key="button1" TargetType="Button">
<Setter Property="Background" Value="Black"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="Margin" Value="20"/>
</Style>
<Style x:Key="button2" TargetType="Button">
<Setter Property="Background" Value="White"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Margin" Value="20"/>
</Style>
</Application.Resources>
</Application>
引用:
MainPage.xaml
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
</UserControl.Resources>
<StackPanel Orientation="Horizontal" Background="red">
<Button Content="Button" Style="{StaticResource button1}" HorizontalAlignment="Left" VerticalAlignment="Top" />
</StackPanel>
</UserControl>
2 局部样式表
MainPage.xaml
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<Style x:Key="button1" TargetType="Button">
<Setter Property="Background" Value="Black"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="Margin" Value="20"/>
</Style>
<Style x:Key="button2" TargetType="Button">
<Setter Property="Background" Value="White"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Margin" Value="20"/>
</Style>
</UserControl.Resources>
<StackPanel Orientation="Horizontal" Background="red">
<Button Content="Button" Style="{StaticResource button1}" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Button Content="Button" Style="{StaticResource button2}" HorizontalAlignment="Left" VerticalAlignment="Top" />
</StackPanel>
</UserControl>
3 内部样式表
MainPage.xaml
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<Style x:Key="button1" TargetType="Button">
<Setter Property="Background" Value="Black"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="Margin" Value="20"/>
</Style>
<Style x:Key="button2" TargetType="Button">
<Setter Property="Background" Value="White"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Margin" Value="20"/>
</Style>
</UserControl.Resources>
<StackPanel Orientation="Horizontal" Background="red">
<Button Content="Button" Style="{StaticResource button1}" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Button Content="Button" Style="{StaticResource button2}" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Button Content="红色" Width="100" Height="80">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="Red"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Margin" Value="20"/>
</Style>
</Button.Style>
</Button>
</StackPanel>
</UserControl>
最新新闻:
· Firefox插件下载量超过20亿(2010-07-03 14:16)
· 微软发布IIS Express:Visual Studio全新内置的Web服务器(2010-07-03 14:08)
· Facebook将加入面部识别和照片标签技术(2010-07-03 14:05)
· 盘点IT界大佬专利:乔布斯一马当先 盖茨重技术(2010-07-03 13:51)
· 最昂贵iPhone 4登场(2010-07-03 13:01)