WPF Style样式 全局样式资源字典

发布于:2025-06-22 ⋅ 阅读:(15) ⋅ 点赞:(0)

在WPF(Windows Presentation Foundation)中,样式(Style)是一种强大的机制,允许开发者定义一组属性值,并将这些属性值应用于一个或多个控件。样式可以简化XAML标记,并使UI的外观和行为更加一致。样式通常在XAML文件的Resources部分中声明,并可以隐式或显式地应用于控件。

 <!--1、将样式注册为资源-->
 <Window.Resources>
     <!--使用Style类创建一个资源-->
     
     <!--TargetType:设置当前的样式是应用于哪个控件的, 将会自动设置所有的Label控件使用这组样式-->
     <Style TargetType="Label">
         <!--在Style中使用Setter控件设置属性-->
         <Setter Property="FontSize" Value="22"/>
         <Setter Property="Foreground" Value="Green"/>
         <!--<Setter Property="Background">
             <Setter.Value>
                 <ImageBrush ImageSource="https://cn.bing.com/th?id=OVFT.dKjW5DKpKzHHiUMwN1np1i"/>
             </Setter.Value>
         </Setter>-->
     </Style>
     
     <!--如果一个样式拥有Key属性,这个样式不会应用到所有的控件,只会应用到显示使用他的控件-->
     <Style TargetType="Button"
            x:Key="btn">
         <Setter Property="Background" Value="red"/>
         <Setter Property="FontWeight" Value="Bold"/>
     </Style>
 <!--当为Style添加Key属性之后,使用这个样式的控件将不会应用默认的样式了-->
 <!--可以使用 BasedOn 属性,让一个Style继承另一个Style,来完成扩展-->
 
 <!--这个例子中继承了默认的TextBox样式,因为默认样式没有Key属性,因此只能通过类型找到继承关系-->
 <Style TargetType="TextBox" 
        x:Key="yellowTB"
        BasedOn="{StaticResource {x:Type TextBox}}"> 
     <Setter Property="Background" Value="#ffff00"/>
 </Style>

继承上一个样式  BasedOn

 <!--如果要继承的目标样式拥有Key属性,则可以直接使用对应的名字进行继承-->
 <Style TargetType="TextBox"
        x:Key="boldTb"
        BasedOn="{StaticResource yellowTB}">
     <Setter Property="FontWeight" Value="Bold"/>
 </Style>

如果要将一个样式应用于不同类型的控件,则需要为他们找到一个公共的基类 FrameworkElement

以上就是一些样式简单的案列 在当前window下定义的样式只会作用于当前的window 当我们需要大量的控件样式 而且其他窗体也需要使用相同的样式 为了方便就需要创建资源字典 邮件项目 添加资源字典  首先可以将上面在 Window 中定义的样式直接剪切ResourceDictionary中。不要复制 <Window.Resources>了,因为现在是给全局的样式下定义,而非仅对于Window中的按钮

<ResourceDictionary xmlns="http://..."
                    xmlns:x="http://...">
    <Style TargetType="Button">
            <Setter Property="Height" Value="50"/>
            <Setter Property="Width" Value="200"/>
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="Margin" Value="10"/>
        </Style>
        <Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Blue"/>
        </Style>
        <Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Red"/>
        </Style>
    <Style x:Key="Forgetstyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Yellow"/>
    </Style>
</ResourceDictionary>

接下来还需要在app.xaml中用上全局样式名.xaml

打开app.xaml,在<Application.Resources>标签中写入

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/你的项目名称;component/资源文件的相对路径"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

完整如下

<Application x:Class="WPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPF_Study"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/WPF/ButtomStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

注意,这里输入的是相对路径。如果这里的资源文件BaseButtomStyle.xaml是放在某个子文件夹下的,那么就应该改成

<ResourceDictionary Source="/WPF/子文件夹/ButtomStyle.xaml"/>

至此,在任何界面中都可以访问到ButtomStyle.xaml中定义的样式。访问方法与之前的局部样式无异

总结

定义局部样式

在需要的窗口的<Window>标签之后添加如下样式定义。

不定义x:Key,则为默认属性。

定义了x:Key,则需要显式地在控件中引用。

定义全局样式

在合适的地方建立资源字典文件:右键项目,添加,资源字典。


网站公告

今日签到

点亮在社区的每一天
去签到