当前位置: 首页 > article >正文

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

目录

  • 前言
  • 一、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.kler.cn/a/536514.html

相关文章:

  • 超详细UE4(虚幻4)第一人称射击(FPS)游戏制作教程
  • git push解决 error src refspec master does not match anyerror
  • 4. 【.NET 8 实战--孢子记账--从单体到微服务--转向微服务】--什么是微服务--微服务设计原则与最佳实践
  • OpenCV:特征检测总结
  • Chapter 4-1. Troubleshooting Congestion in Fibre Channel Fabrics
  • [250202] DocumentDB 开源发布:基于 PostgreSQL 的文档数据库新选择 | Jekyll 4.4.0 发布
  • ubuntu和手机之间如何传递消息
  • Spider 数据集上实现nlp2sql训练任务
  • SpringCloud面试题----SpringCloud和Dubbo有什么区别
  • Synchronized和ReentrantLock面试详解
  • 第4章 Jetpack Compose提供了一系列的布局组件
  • 【Elasticsearch】分桶聚合功能概述
  • Windows上工程组织方式 --- dll插件式
  • 本地缓存怎么保证数据一致性?
  • pikachu[皮卡丘] 靶场全级别通关教程答案 以及 学习方法 如何通过渗透测试靶场挑战「pikachu」来精通Web渗透技巧? 一篇文章搞完这些问题
  • 高级测试工程师,在数据安全方面,如何用AI提升?DeepSpeek的回答
  • iOS pod install一直失败,访问github超时记录
  • LabVIEW位移测量系统
  • 06vue3实战-----项目开发准备
  • windows部署本地deepseek
  • arkui-x 鼠标切换为键盘,焦点衔接问题
  • 【实战篇】DeepSeek + Cline 编程实战:从入门到“上头”
  • STM32上部署AI的两个实用软件——Nanoedge AI Studio和STM32Cube AI
  • 流媒体缓存管理策略
  • Python的那些事第十四篇:Flask与Django框架的趣味探索之旅
  • 阿里云cdn怎样设置图片压缩