【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>
相关推荐
Full Stack Developme5 分钟前
Hutool CollUtil 教程
java·开发语言·windows·python
Shadow(⊙o⊙)11 分钟前
mkfifo()命名管道-FIFO客户端 服务端模拟。*System V消息队列、信号量(信号灯)。
linux·运维·服务器·开发语言·c++
zfoo-framework14 分钟前
kotlin中体会到一些比较好用的点
android·开发语言·kotlin
赵谨言15 分钟前
基于C#的在线编码与自动化测试全栈Web平台的设计与实现
开发语言·前端·c#
牛油果子哥q21 分钟前
C++六大默认成员函数深度精讲:构造/析构/拷贝/赋值/移动构造/移动赋值、编译器生成规则、深浅拷贝终极坑点与工程实战
开发语言·c++
Shadow(⊙o⊙)23 分钟前
System V共享内存详解,shm系列接口,三种共享内存删除机制。System V通信缺点分析
linux·运维·服务器·开发语言·网络·c++
ZC跨境爬虫25 分钟前
跟着 MDN 学JavaScript day_4:如何存储你需要的信息——变量
开发语言·前端·javascript·ui·ecmascript
1892280486125 分钟前
NV077固态MT29F16T08ESLCHL6-QAES:C
c语言·开发语言·性能优化
小小de风呀27 分钟前
de风——【从零开始学C++】(十三):优先级队列 priority_queue 全解析 & 仿函数入门
开发语言·c++
糖果店的幽灵29 分钟前
时间序列处理
开发语言·python·pandas