WPF样式

WPF样式-Style样式玩法

刚学WPF的时候,很多人都会写出这样的界面代码:

<Button Content="登录" Height="50" Width="100" FontSize="20" Background="Green"/>
<Button Content="退出" Height="50" Width="100" FontSize="20" Background="Green"/>

这样写,看似没有什么问题,但只要按钮一多,你就会发现一个问题:很多属性都在重复写。这个时候WPF 的 Style 样式就派上用场了。简单来说:Style 的作用,就是把一组重复的属性设置提取出来,统一定义,统一使用。

一、Style基础用法

1.写进 Window.Resources 中,无X:key:

<Window.Resources>
    <Style TargetType="Button" >
        <Setter Property="Height" Value="50"/>
        <Setter Property="Width" Value="100"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Background" Value="Green"/>
    </Style>
</Window.Resources>

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button Content="登录"/>
    <Button Content="退出"/>
</StackPanel>

由于没有X:key,说明这个样式是给当前窗体的Button用的
效果:
image

2.命名Style (x:Key)

上面的写法有一个问题,所有的Button按钮都是一个样式,但往往现实中,需要的是不同样式的Button按钮;设置x:Key就能解决这个问题

<Window.Resources>
    <Style x:Key="Login" TargetType="Button">
        <Setter Property="Height" Value="70"/>
        <Setter Property="Width" Value="120"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Background" Value="Green"/>
    </Style>

    <Style x:Key="Quit" TargetType="Button">
        <Setter Property="Height" Value="50"/>
        <Setter Property="Width" Value="100"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Background" Value="Red"/>
    </Style>
</Window.Resources>

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button Content="登录" Style="{StaticResource Login}"/>
    <Button Content="退出" Style="{StaticResource Quit}"/>
</StackPanel>

x:Key:设置样式的名称,Style="{StaticResource Login},按钮使用Login的样式;

  • x:Key 负责定义资源名字,StaticResource 负责按名字取资源。

效果:
image

3.样式继承(BasedOn)

上面代码中Login 和 Quit 两个样式,如果有相同属性就可以采用继承,比如:高度、宽度、字号

<Window.Resources>
    <Style TargetType="Button">
        <Setter Property="Height" Value="70"/>
        <Setter Property="Width" Value="120"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Background" Value="White"/>
    </Style>

    <Style x:Key="Login" TargetType="Button"
       BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Green"/>
    </Style>

    <Style x:Key="Quit" TargetType="Button"
       BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Red"/>
    </Style>
</Window.Resources>

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <Button Content="登录" Style="{StaticResource Login}"/>
    <Button Content="退出" Style="{StaticResource Quit}"/>
</StackPanel>

BasedOn 就是指定当前样式继承于哪个已有样式;如果子样式里设置了同名属性,会覆盖父样式里的值。
效果:
image

4.StaticResource 和 DynamicResource 的区别

在引用样式时,我们有两种方式,分别是DynamicResource和StaticResource,后面再写上样式的名称。DynamicResource表示动态资源,StaticResource表示静态资源。这两者的区别是:静态资源在第一次编译后即确定其对象或值,之后不能对其进行修改。动态资源则是在运行时决定,当运行过程中真正需要时,才到资源目标中查找其值。因此,我们可以动态地修改它。由于动态资源的运行时才能确定其值,因此效率比静态资源要低。

<Window.Resources>
    <!-- 定义一个资源 -->
    <SolidColorBrush x:Key="BtnBgBrush" Color="Orange"/>
</Window.Resources>

<StackPanel Margin="30" VerticalAlignment="Center">

    <!-- 使用 StaticResource -->
    <Button Content="我是 StaticResource"
            Width="200"
            Height="45"
            Margin="10"
            Background="{StaticResource BtnBgBrush}"/>

    <!-- 使用 DynamicResource -->
    <Button Content="我是 DynamicResource"
            Width="200"
            Height="45"
            Margin="10"
            Background="{DynamicResource BtnBgBrush}"/>

    <Button Content="修改资源颜色"
            Width="200"
            Height="45"
            Margin="10"
            Click="Button_Click"/>

</StackPanel>

后台资源:

private void Button_Click(object sender, RoutedEventArgs e)
{
    // 修改资源 BtnBgBrush
    this.Resources["BtnBgBrush"] = new SolidColorBrush(Colors.SkyBlue);
}

效果:

QQ20260425-211357-HD

5.写在字典资源文件中

在整个项目的窗体有效

第一步、创建资源字典

image

第二步、把Style写在资源字典文件中

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button">
        <Setter Property="Height" Value="70"/>
        <Setter Property="Width" Value="120"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Background" Value="White"/>
        <Setter Property="Margin" Value="3"/>
    </Style>

    <Style x:Key="Login" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Content" Value="Login"/>
        <Setter Property="Background" Value="Green"/>
    </Style>

    <Style x:Key="Quit" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Content" Value="Quit"/>
        <Setter Property="Background" Value="Red"/>
    </Style>
</ResourceDictionary>

第三步、在App.xaml中引入该资源字典

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="Btn_Style.xaml">
    <Application.Resources>
        <!--资源字典-->
        <ResourceDictionary>
            <!--画笔 -->
            <SolidColorBrush x:Key="ButtonBackground" Color="Blue"/>
            <SolidColorBrush x:Key="ButtonForeground" Color="White"/>
            <!--样式-->
            <Style x:Key="ButtonStyle" TargetType="Button">
                <Setter Property="Width" Value="100"/>
                <Setter Property="Height" Value="30"/>
                <Setter Property="Background" Value="{StaticResource ButtonBackground}"/>
                <Setter Property="Foreground" Value="{StaticResource ButtonForeground}"/>
                <Setter Property="Margin" Value="3"/>
            </Style>

            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/BaseButtonDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

我们在Application的Resources属性中实例化了一个ResourceDictionary对象,并在其中定义了两个SolidColorBrush对象,一个style样式,以及在MergedDictionaries集合中添加了一个ResourceDictionary对象,其数据源指向了BaseButtonDictionary.xaml资源文件。

第四步、窗体直接使用

<Window.Resources>
        <Style x:Key="PinkButtonStyle" TargetType="Button">
            <Setter Property="Width" Value="100"/>
            <Setter Property="Height" Value="30"/>
            <Setter Property="Background" Value="Pink"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Margin" Value="3"/>
        </Style>
    </Window.Resources>
    <StackPanel VerticalAlignment="Center">
        <Button Content="蓝色按钮" Style="{StaticResource ButtonStyle}"/>
        <Button Content="绿色按钮" Style="{StaticResource Login}"/>
        <Button Content="红色色按钮" Style="{StaticResource Quit}"/>
        <Button Content="粉色按钮" Style="{StaticResource PinkButtonStyle}"/>
        <Button Content="橙色按钮">
            <Button.Style>
                <Style TargetType="Button">
                    <Setter Property="Width" Value="100"/>
                    <Setter Property="Height" Value="30"/>
                    <Setter Property="Background" Value="Orange"/>
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="Margin" Value="3"/>
                </Style>
            </Button.Style>
        </Button>
    </StackPanel>

效果:

image

二、WPF触发器

2.1、什么是 Trigger 触发器

如果说 Style 解决的是“静态样式”问题,那么 Trigger 解决的就是“动态变化”问题。
比如:

  • 鼠标移到按钮上,颜色变了
  • 按钮被按下时,大小变了
  • 某个状态成立时,控件自动切换样式
    Trigger:当满足预设条件时,自动执行一些事务

2.2、Trigger的属性

  • Property属性:表示定义触发器所指向的属性名称,需要注意的是,它的类似是DependencyProperty(依赖属性),也就是说,触发器所生效的属性必须是WPF中的依赖属性。

  • Value属性:表示定义触发器所指向的属性的值,这两个属性要连起来理解,即某个属性的值等于预设的值时,此时将该触发器将被触发,至于要执行哪些具体的事务,就要看Setters集合中定义了哪些项。

  • SourceName属性:获取或设置与导致关联的 setter 要应用的属性对象的名称。

  • Setters属性:这是一个集合,类似于Style样式的Setters,且用途是一致的,就是指当前触发器被触发时,将执行Setters里面的内容项

2.3、Trigger的示例

需要注意的是:Trigger触发器的条件应该是当前控件拥有的属性名称

 <Window.Resources>
     <Style x:Key="GreenButtonStyle" TargetType="Button">
         <Setter Property="Width" Value="100"/>
         <Setter Property="Height" Value="30"/>
         <Setter Property="Background" Value="Green"/>
         <Setter Property="Foreground" Value="White"/>
         <Setter Property="Content" Value="绿色按钮"/>
         <Setter Property="Margin" Value="3"/>
         <Style.Triggers>
             <Trigger Property="IsMouseOver" Value="True">
                 <Setter Property="Foreground" Value="Red"/>
                 <Setter Property="Width" Value="150"/>
                 <Setter Property="Height" Value="50"/>
                 <Setter Property="Content" Value="鼠标移入"/>
             </Trigger>
         </Style.Triggers>
     </Style>
 </Window.Resources>
 <StackPanel VerticalAlignment="Center">
     <Button Style="{StaticResource GreenButtonStyle}"/>
 </StackPanel>

Trigger 监听的属性,必须是当前控件拥有的依赖属性。例如 Button 有 IsMouseOver,所以可以监听它;但如果写一个 Button 根本没有的属性,就会报错。
公式:Trigger = 监听一个属性 + 当属性值满足条件时执行一组 Setter
效果:
QQ20260425-221537-HD

三、总结

Style 负责统一外观,ResourceDictionary 负责统一管理,Trigger 负责动态变化。

posted @ 2026-04-25 22:20  大眼邓小眼睛  阅读(39)  评论(0)    收藏  举报