【C# WPF】Style全局样式和资源字典

1.全局样式:

在Window.Resource中声明一个样式,总体为白色,为了更有区分度,采用BasedOn这一继承方式来在保留字体和边缘设置的基础上,更改颜色。

xml 复制代码
<Window x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="Day2" Height="450" Width="800">

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Background" Value="White"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Margin" Value="10"/>
        </Style>

        <Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Red"/>
        </Style>
        <Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Pink"/>
        </Style>

    </Window.Resources>
    
    <StackPanel>
        <Button Content="登录" Style="{StaticResource LoginStyle}"/>
        <Button Content="退出" Style="{StaticResource QuitStyle}"/>
    </StackPanel>
        
</Window>

窗体结构:

2.资源字典

第一种方法,仅能将样式应用在本窗体中,不符合封装、多用的思想。

所以要将Style资源样式分离出来,静态调用。

在项目上,右击添加--资源字典,在资源字典中添加Style类型:

xml 复制代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style TargetType="Button">
        <Setter Property="Background" Value="White"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Margin" Value="10"/>
    </Style>

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

这样就将资源样式添加到了资源字典中。

下面是如何调用这些写好的样式,找到App.xaml,在<Application.Resources>中添加:

xml 复制代码
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="WpfApp1;component/BaseButtonStyle.xaml">
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

这样就被全局包含了。

测试一下,跟之前是一样的,并且在其他Window中也能调用。

第一个窗口:

xml 复制代码
    <StackPanel>
        <Button Content="登录" Style="{StaticResource LoginStyle}"/>
        <Button Content="退出" Style="{StaticResource QuitStyle}"/>
    </StackPanel>

其他新建窗口:

xml 复制代码
    <Grid>
        <Button Style="{StaticResource LoginStyle}"/>
    </Grid>
相关推荐
yeziyfx7 分钟前
kotlin中集合的用法
android·开发语言·kotlin
lph197224 分钟前
快速分页wpf
c#
三体世界1 小时前
TCP传输控制层协议深入理解
linux·服务器·开发语言·网络·c++·网络协议·tcp/ip
随心点儿1 小时前
使用python 将多个docx文件合并为一个word
开发语言·python·多个word合并为一个
不学无术の码农1 小时前
《Effective Python》第十三章 测试与调试——使用 Mock 测试具有复杂依赖的代码
开发语言·python
tomcsdn311 小时前
SMTPman,smtp的端口号是多少全面解析配置
服务器·开发语言·php·smtp·邮件营销·域名邮箱·邮件服务器
试行1 小时前
C#System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误。
c#
EnigmaCoder1 小时前
Java多线程:核心技术与实战指南
java·开发语言
❀always❀3 小时前
深入浅出分布式限流(更新中)
分布式·wpf
大白的编程日记.3 小时前
【计算机基础理论知识】C++篇(二)
开发语言·c++·学习