【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>
相关推荐
野生技术架构师4 分钟前
深度拆解JVM垃圾回收:可达性分析原理+全类型回收器执行机制
java·开发语言·jvm
缺点内向6 分钟前
在 C# 中为 Word 段落添加制表位:使用 Spire.Doc for .NET 实现高效排版
开发语言·c#·自动化·word·.net
中科院提名者7 分钟前
如何配置go环境并用vscode运行
开发语言·后端·golang
电饭叔12 分钟前
GUI by Python 6 一段 gui 代码分析
开发语言·python
赤水无泪31 分钟前
03 C++语言---预处理器
开发语言·c++
星空下的月光影子31 分钟前
易语言开发从入门到精通:补充篇·易语言与物联网(IoT)深度实践·ESP8266本地MQTT通信·数据采集存储·Windows端可视化监控平台
开发语言
老骥伏枥~32 分钟前
【C# 入门】变量、常量与命名规范
开发语言·c#
weixin_4407841133 分钟前
Java线程池工作原理浅析
android·java·开发语言·okhttp·android studio·android runtime
2401_8321319542 分钟前
模板编译期机器学习
开发语言·c++·算法
嵌入小生00742 分钟前
Data Structure Learning: Starting with C Language Singly Linked List
c语言·开发语言·数据结构·算法·嵌入式软件