WPF 四种样式

1、内联样式
<TextBlock FontSize="20" Foreground="Blue">好啊</TextBlock>


2、页面样式
<Button Height="20" Margin="0,78,445,213>按钮1</Button>
<Button Width="50" Height="20" Margin="0,116,453,175">按钮2</Button>

<Window.Resources>
<Style TargetType="Button" x:Key="MyStyle">
<Setter Property="Foreground" Value="Blue"></Setter>
<Setter Property="FontSize" Value="30"></Setter>
</Style>
</Window.Resources>

应用特性名称样式:
在样式中添加x:key属性
<Style TargetType="Button" x:Key="MyStyle">
<Setter Property="Foreground" Value="BlanchedAlmond"></Setter>
<Setter Property="FontSize" Value="30"></Setter>
</Style>

然后应用该样式 <Button Height="20" Margin="0,78,445,213" Style="{StaticResource MyStyle}">按钮1</Button>

3、全局样式
在App.xaml中添加
<Application.Resources>
<Style TargetType="Button" x:Key="MyStyle">
<Setter Property="Foreground" Value="Blue"></Setter>
<Setter Property="FontSize" Value="30"></Setter>
</Style>
</Application.Resources>

4、外部样式
新建Dictionary1.xaml文件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button" x:Key="MyStyle">
<Setter Property="Foreground" Value="Blue"></Setter>
<Setter Property="FontSize" Value="30"></Setter>
</Style>
</ResourceDictionary>

在引用页面添加
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>

 

posted @ 2014-07-26 21:49  瓜王  阅读(1206)  评论(0编辑  收藏  举报