记录 | 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之间区别。
相关推荐
张同学的IT技术日记19 分钟前
线性代数于工程应用中的实践:以代码实例拆解相似性度量问题的求解逻辑
开发语言·笔记·python·学习·线性代数·工程应用
张同学的IT技术日记19 分钟前
线性代数于工程应用中的实践:以代码实例拆解图像平滑问题的求解逻辑
开发语言·笔记·python·学习·线性代数·工程应用
思麟呀1 小时前
栈和队列的实现(C语言)
c语言·数据结构·学习
F——1 小时前
2025 IT职业发展方向及推荐
运维·数据库·学习·云计算·边缘计算
蝴蝶不愿意2 小时前
Java基础学习笔记-static关键字
java·开发语言·学习
互联网打工人no12 小时前
WPF模板
wpf
bae-唯一3 小时前
深入了解 MySQL:从基础到高级特性
数据库·学习·mysql·命令
^@^lemon tea^@^5 小时前
WPF 在后台使TextBox失去焦点的方法
wpf·wpf textbox失去焦点