记录 | 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之间区别。
相关推荐
DKPT15 分钟前
Java享元模式实现方式与应用场景分析
java·笔记·学习·设计模式·享元模式
Green1Leaves41 分钟前
pytorch学习-11卷积神经网络(高级篇)
pytorch·学习·cnn
DKPT8 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
好好研究10 小时前
学习栈和队列的插入和删除操作
数据结构·学习
新中地GIS开发老师11 小时前
新发布:26考研院校和专业大纲
学习·考研·arcgis·大学生·遥感·gis开发·地理信息科学
SH11HF12 小时前
小菜狗的云计算之旅,学习了解rsync+sersync实现数据实时同步(详细操作步骤)
学习·云计算
Frank学习路上12 小时前
【IOS】XCode创建firstapp并运行(成为IOS开发者)
开发语言·学习·ios·cocoa·xcode
Chef_Chen13 小时前
从0开始学习计算机视觉--Day07--神经网络
神经网络·学习·计算机视觉
X_StarX15 小时前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生
MingYue_SSS15 小时前
开关电源抄板学习
经验分享·笔记·嵌入式硬件·学习