记录 | WPF基础学习Style局部和全局调用

news/2025/2/8 11:36:19/

目录

  • 前言
  • 一、Style
    • 1.1 例子
    • 1.2 为样式起名字
    • 1.3 BasedOn 继承上一个样式
  • 二、外部Style
    • Step1 创建资源字典BaseButtonStyle.xaml
    • Step2 在资源字典中写入Style
    • Step3 App.xaml中写引用路径【全局】
    • Step4 调用
    • 三、代码提供
    • 四、x:Key和x:Name区别
  • 更新时间


前言

参考文章:
参考视频:【WPF入门教程 Visual Studio 2022】WPF界面开发入门

自己的感想


一、Style

如下图所示,在Windows.Resources中设置的Style属性,只在当前界面产生效果。
在这里插入图片描述

1.1 例子

在这里插入图片描述

<Window x:Class="WPF_Study.MainWindow"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:WPF_Study"mc:Ignorable="d"Title="WPF入门.txt" Height="600" Width="800"><Window.Resources><Style TargetType="Button"><Setter Property="Background" Value="Red"/><Setter Property="FontSize" Value="20"/><Setter Property="Height" Value="40"/><Setter Property="Width" Value="70"/></Style></Window.Resources><StackPanel><Button Content="登录" /><Button Content="帮助" /><Button Content="退出" /></StackPanel></Window>

1.2 为样式起名字

为啥要起别名?1.1中的案例导致所有的Button组件都是这个红色样式,但是如果我们只想让退出按钮为红色,其他三个按钮为绿色,该怎么操作?
在这里插入图片描述

<Window x:Class="WPF_Study.MainWindow"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:WPF_Study"mc:Ignorable="d"Title="WPF入门.txt" Height="600" Width="800"><Window.Resources><Style x:Key="QuitStyle" TargetType="Button"><Setter Property="Background" Value="Red"/><Setter Property="FontSize" Value="20"/><Setter Property="Height" Value="40"/><Setter Property="Width" Value="70"/></Style><Style x:Key="loginStyle" TargetType="Button"><Setter Property="Background" Value="Green"/><Setter Property="FontSize" Value="20"/><Setter Property="Height" Value="40"/><Setter Property="Width" Value="70"/></Style></Window.Resources><StackPanel><Button Style="{StaticResource loginStyle}" Content="登录" /><Button Style="{StaticResource loginStyle}" Content="帮助" /><Button Style="{StaticResource QuitStyle}" Content="退出" /></StackPanel></Window>

1.3 BasedOn 继承上一个样式

在这里插入图片描述

<Window x:Class="WPF_Study.MainWindow"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:WPF_Study"mc:Ignorable="d"Title="WPF入门.txt" Height="600" Width="800"><Window.Resources><Style TargetType="Button"><Setter Property="Background" Value="WhiteSmoke"/><Setter Property="FontSize" Value="20"/><Setter Property="Margin" Value="20,10"/></Style><Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Background" Value="Red"/></Style><Style x:Key="loginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Background" Value="Green"/></Style></Window.Resources><StackPanel><Button Style="{StaticResource loginStyle}" Content="登录" /><Button Content="帮助" /><Button Style="{StaticResource QuitStyle}" Content="退出" /></StackPanel></Window>

二、外部Style

需要我们把style放入xaml文件中。

Step1 创建资源字典BaseButtonStyle.xaml

在这里插入图片描述

Step2 在资源字典中写入Style

在这里插入图片描述

Step3 App.xaml中写引用路径【全局】

在这里插入图片描述

Step4 调用

在这里插入图片描述


三、代码提供

C# WPF中的Style写入xaml中进行全局引用——点击下载代码


四、x:Key和x:Name区别

  • 在Resources中进行唯一标定。

在这里插入图片描述
在这里插入图片描述


更新时间

  • 2025-02-06:创建。
  • 2025-02-07:补充x:key和x:name之间区别。

http://www.ppmy.cn/news/1570312.html

相关文章

Unity之VideoPlayer视频播放(二)

一、效果 全屏缩小画面、播放暂停、进度条拖拽、视频播放时长、倍速、音量等功能。 长时间不移动鼠标自动隐藏播放控制器、鼠标离开倍速、音量时隐藏倍速、音量ui 二、脚本 using System; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;…

Maven架构项目管理工具

1.1什么是Maven 翻译为“专家”&#xff0c;“内行”Maven是跨平台的项目管理工具。主要服务于基于Java平台的项目构建&#xff0c;依赖管理和项目信息管理。什么是理想的项目构建&#xff1f; 高度自动化&#xff0c;跨平台&#xff0c;可重用的组件&#xff0c;标准化的 什么…

开源AI智能名片2 + 1链动模式S2B2C商城小程序:内容价值创造与传播新引擎

摘要&#xff1a;本文聚焦于信息爆炸时代下&#xff0c;内容价值的创造与传播。随着用户角色的转变&#xff0c;其在内容生产与传播中的价值日益凸显。同时&#xff0c;深入探讨开源AI智能名片2 1链动模式S2B2C商城小程序这一创新商业模式&#xff0c;如何借助用户创造内容并传…

并行计算、分布式计算与云计算:概念剖析与对比研究(表格对比)

什么是并行计算&#xff1f;什么是分布计算&#xff1f;什么是云计算&#xff1f;我们如何更好理解这3个概念&#xff0c;我们采用概念之间的区别和联系的方式来理解&#xff0c;做到切实理解&#xff0c;深刻体会。 1、并行计算与分布式计算 并行计算、分布式计算都属于高性…

【算法专场】分治(下)

目录 前言 归并排序 思想 912. 排序数组 算法思路 算法代码 LCR 170. 交易逆序对的总数 算法思路 算法代码 315. 计算右侧小于当前元素的个数 - 力扣&#xff08;LeetCode&#xff09; 算法思路 算法代码 493. 翻转对 算法思路 算法代码 好久不见~时隔多日&…

使用 Axios ——个人信息修改与提示框实现

目录 详细介绍&#xff1a;个人信息设置与修改页面实现 1. HTML 结构 2. CSS 样式 3. JavaScript 核心逻辑 a. 信息渲染与表单提交 b. 头像上传与预览 4. 功能详解 5. 总结 提示&#xff1a; 这段代码展示了如何创建一个简单的个人信息设置页面&#xff0c;包含用户个…

C# LiteDB 使用教程

一、引言 在软件开发中&#xff0c;数据存储和管理是至关重要的一环。对于小型项目或者对性能和便捷性有较高要求的场景&#xff0c;传统的大型数据库可能显得过于笨重。而 LiteDB 作为一款轻量级的嵌入式 NoSQL 数据库&#xff0c;为开发者提供了一个简洁、高效的解决方案。它…

集合类不安全问题

ArrayList不是线程安全类&#xff0c;在多线程同时写的情况下&#xff0c;会抛出java.util.ConcurrentModificationException异常 解决办法&#xff1a; 1.使用Vector&#xff08;ArrayList所有方法加synchronized&#xff0c;太重&#xff09; 2.使用Collections.synchronized…