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

目录

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

前言

参考文章:

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

自己的感想


一、Style

如下图所示,在Windows.Resources中设置的Style属性,只在当前界面产生效果。

1.1 例子

csharp 复制代码
<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组件都是这个红色样式,但是如果我们只想让退出按钮为红色,其他三个按钮为绿色,该怎么操作?

csharp 复制代码
<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 继承上一个样式

csharp 复制代码
<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之间区别。
相关推荐
psybrain5 分钟前
脑科学圈| 利用眼动追踪评估演讲情境下焦虑障碍儿童的注视行为
学习·心理学·脑科学·课堂·焦虑·儿童青少年·眼动
序属秋秋秋1 小时前
《C++初阶之内存管理》【内存分布 + operator new/delete + 定位new】
开发语言·c++·笔记·学习
许白掰1 小时前
Linux入门篇学习——Linux 工具之 make 工具和 makefile 文件
linux·运维·服务器·前端·学习·编辑器
B1nna2 小时前
Docker学习
学习·docker·容器
promising-w8 小时前
【运算放大器专题】基础篇
嵌入式硬件·学习
宝山哥哥8 小时前
网络信息安全学习笔记1----------网络信息安全概述
网络·笔记·学习·安全·网络安全
前端开发与ui设计的老司机8 小时前
从UI设计到数字孪生实战:构建智慧教育的个性化学习平台
学习·ui
X Y O9 小时前
神经网络初步学习3——数据与损失
人工智能·神经网络·学习
霖0011 小时前
C++学习笔记三
运维·开发语言·c++·笔记·学习·fpga开发
巴伦是只猫12 小时前
【机器学习笔记 Ⅲ】1 无监督学习
笔记·学习·机器学习