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

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

cs 复制代码
 <!--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>
cs 复制代码
 <!--当为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

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

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

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

cs 复制代码
<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>标签中写入

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

完整如下

cs 复制代码
<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是放在某个子文件夹下的,那么就应该改成

cs 复制代码
<ResourceDictionary Source="/WPF/子文件夹/ButtomStyle.xaml"/>

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

总结

定义局部样式

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

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

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

定义全局样式

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

相关推荐
A_nanda1 小时前
c# 用VUE+elmentPlus生成简单管理系统
javascript·vue.js·c#
wuguan_2 小时前
C#之线程
开发语言·c#
gc_22994 小时前
学习C#调用OpenXml操作word文档的基本用法(21:学习嵌入对象类)
c#·word·openxml·ole
老骥伏枥~4 小时前
C# if / else 的正确写法与反例
开发语言·c#
老骥伏枥~4 小时前
C# 运算符优先级易踩坑
c#
Poetinthedusk4 小时前
WPF应用跟随桌面切换
开发语言·wpf
SunnyDays10115 小时前
C# 实战:从 Word 文档中提取指定页面
c#·提取word文档页面·将word页面复制到另一个文档
骆驼爱记录5 小时前
Word通配符技巧:高效文档处理指南
开发语言·c#·自动化·word·excel·wps·新人首发
人工智能AI技术5 小时前
智能体元年第一课:使用Microsoft Foundry + AI Toolkit 10分钟构建你的第一个多模态Agent
人工智能·c#
柱子jason5 小时前
基于IOT-Tree Server支持的gRPC服务,使用C#开发自己的设备监控客户端
物联网·rpc·c#·自动化·iiot·iot-tree