首页 / 软件开发 / Silverlight / 稳扎稳打Silverlight(12) - 2.0外观之样式,模板,视觉状态和视觉状态管理器
稳扎稳打Silverlight(12) - 2.0外观之样式,模板,视觉状态和视觉状态管理器2010-04-26 cnblogs webabcd在线DEMOhttp://www.cnblogs.com/webabcd/archive/2008/10/09/1307486.html示例1、样式(App.xaml)<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Silverlight20.App"
>
<Application.Resources>
<!--全局样式 - 任何地方都可引用-->
<!--
Style - 自定义样式资源。用于修改控件的样式。各个控件的默认样式可参见文档
x:Key - 唯一标识
TargetType - 目标对象类型
Setter - 属性设置器
Property - 需要设置的属性名称
Value - 需要设置的属性值
-->
<Style x:Key="styleTestApp" TargetType="TextBox">
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="#0000FF"/>
</Style>
</Application.Resources>
</Application>
样式(Style.xaml)<UserControl x:Class="Silverlight20.Appearance.Style"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left">
<StackPanel.Resources>
<!--容器内样式 - 所属容器内可引用-->
<!--
Style - 自定义样式资源。用于修改控件的样式。各个控件的默认样式可参见文档
x:Key - 唯一标识
TargetType - 目标对象类型
Setter - 属性设置器
Property - 需要设置的属性名称
Value - 需要设置的属性值
-->
<Style x:Key="styleTestInContainer" TargetType="TextBox">
<Setter Property="FontSize" Value="24"/>
<Setter Property="Foreground" Value="#00FF00"/>
</Style>
</StackPanel.Resources>
<!--全局样式的应用-->
<TextBox Text="我是TextBox(全局样式的应用)" Margin="5" Style="{StaticResource styleTestApp}" />
<!--容器内样式的应用-->
<TextBox Text="我是TextBox(容器内样式的应用)" Margin="5" Style="{StaticResource styleTestInContainer}" />
<!--内联样式的应用。内联样式优先级高于全局样式和容器内样式-->
<TextBox Text="我是TextBox(内连样式的应用)" Margin="5" Foreground="#FF0000" Style="{StaticResource styleTestInContainer}" />
</StackPanel>
</UserControl>