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,则需要显式地在控件中引用。

定义全局样式

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

相关推荐
缺点内向34 分钟前
C#: 告别繁琐!轻松移除Word文档中的文本与图片水印
c#·自动化·word·.net
喵叔哟1 小时前
06-ASPNETCore-WebAPI开发
服务器·后端·c#
2501_930707782 小时前
使用 C# .NET 从 PowerPoint 演示文稿中提取背景图片
c#·powerpoint·.net
初级代码游戏2 小时前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
大空大地20263 小时前
流程控制语句--switch多分支语句使用、while循环语句的使用、do...while语句、for循环
c#
kylezhao20195 小时前
C#序列化与反序列化详细讲解与应用
c#
JQLvopkk5 小时前
C# 实践AI :Visual Studio + VSCode 组合方案
人工智能·c#·visual studio
故事不长丨5 小时前
C#线程同步:lock、Monitor、Mutex原理+用法+实战全解析
开发语言·算法·c#
kingwebo'sZone5 小时前
C#使用Aspose.Words把 word转成图片
前端·c#·word
大空大地20266 小时前
表达式与运算符
c#