原文链接:http://blog.csdn.net/pan_junbiao/article/details/50987932
样式的定义
样式的元素名称为Style,通过TargetType来声明作用的目标的类型,通过Setter来声明作用目标的属性,一个Style中可以添加多个Setter,在控件中使用Style的x:key值来引用这个样式,下面实现对按钮的样式定义。
1.1 定义样式
<Window.Resources>
<!--定义按钮公共样式-->
<Style x:Key="gBut" TargetType="{x:Type Button}">
<!--手型-->
<Setter Property="Cursor" Value="Hand"/>
<!--宽度-->
<Setter Property="Width" Value="150"/>
<!--长度-->
<Setter Property="Height" Value="80"/>
</Style>
<!--定义按钮样式1-->
<Style x:Key="cBtn1" TargetType="{x:Type Button}" BasedOn="{StaticResource gBut}">
<!--背景色-->
<Setter Property="Background" Value="Black"/>
<!--字号-->
<Setter Property="FontSize" Value="24"/>
<!--字颜色-->
<Setter Property="Foreground" Value="Blue"/>
<!--字间距-->
<Setter Property="Margin" Value="20"/>
</Style>
<!--定义按钮样式2-->
<Style x:Key="cBtn2" TargetType="{x:Type Button}" BasedOn="{StaticResource gBut}">
<!--背景色-->
<Setter Property="Background" Value="White"/>
<!--字号-->
<Setter Property="FontSize" Value="24"/>
<!--字颜色-->
<Setter Property="Foreground" Value="Red"/>
<!--字间距-->
<Setter Property="Margin" Value="10"/>
</Style>
</Window.Resources>
BasedOn属性:通过设置BasedOn属性,可以继承某个样式。
1.2 引用样式
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<!--应用样式1的按钮-->
<Button Content="蓝色" Style="{StaticResource cBtn1}"/>
<!--应用样式2的按钮-->
<Button Content="白色" Style="{StaticResource cBtn2}"/>
<!--应用样式3的按钮-->
<Button Content="红色">
<Button.Style>
<!--定义按钮样式3-->
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource gBut}">
<!--背景色-->
<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>
样式的作用域
1、全局样式
全局样式:只要把样式元素写在App.xaml中即可。
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<!--全局应用程序资源-->
<Application.Resources>
<!--定义全局样式-->
</Application.Resources>
</Application>
2、局部样式
<!--窗口资源-->
<Window.Resources>
<!--定义局部样式-->
</Window.Resources>
3、内部样式
<Button Content="红色" Width="150" Height="80">
<Button.Style>
<!--定义内部-->
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource gBut}">
<!--背景色-->
<Setter Property="Background" Value="Red"/>
<!--字号-->
<Setter Property="FontSize" Value="24"/>
</Style>
</Button.Style>
</Button>
以上的样式引用时的方法是一样的,都是使用Resources,内部样式的使用仅限于改控件本身,因为它没有被放到资源字典之中,xxx.Resources是支持向下继承的,并可以应用它在资源中的定义样式,例如:
<Grid>
<!--定义Grid控件的资源-->
<Grid.Resources>
<!--定义按钮样式1-->
<Style x:Key="grid_button" TargetType="{x:Type Button}">
<Setter Property="Width" Value="260"/>
<Setter Property="Height" Value="160"/>
<Setter Property="FontSize" Value="26"/>
<Setter Property="Content" Value="应用父级样式"/>
</Style>
</Grid.Resources>
<!--应用父级样式的按钮-->
<Button Style="{StaticResource grid_button}"></Button>
</Grid>
运行结果所示,XAML代码中的按钮除了Style=”{StaticResource grid_button}”之外并没有声明任何属性,按钮的尺寸和内容都是通过应用父级Grid资源样式来呈现的,所以只要父级的对象定义了资源,父级以下的元素均可以访问它的资源字典。
WPF如何引用外部样式 http://www.cnblogs.com/ahcc08/p/6220303.html
当我们给一些控件设置相同的属性的时候,这时候,我们可以把这些属性写到一个Style里面。而其他页面也有类似的控件也需要使用这个Style,这时候就需要把这个Style放在一个共通的文件里,然后引入这个Style文件即可。有点像html引入CSS文件一样。
首先新建一个资源字典文件,方法如下:
然后在里面添加我们的样式,如简单写一个Button的样式:
<Style x:Key="BtnStyle" TargetType="Button">
<Setter Property="Height" Value="72" />
<Setter Property="Width" Value="150" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Green" />
</Style>
然后在我们的窗体里引用:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/test.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
这里的Soure里写的是相对路径,因为我的test.xmal是放在Style文件夹里的,所以前面需要加Style。
使用的方法,如普通的方法是一样的。
<Button x:Name="button" Grid.Column="1" Grid.Row="1" Style="{DynamicResource BtnStyle}" />
效果如下:
有关定位的样式不要整理到样式表中,例如:
Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Grid.RowSpan="2"