记录 | 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之间区别。
相关推荐
我真不会起名字啊33 分钟前
OpenSceneGraph(OSG)开发学习
学习
永日4567033 分钟前
学习日记-spring-day42-7.7
java·学习·spring
Love__Tay4 小时前
笔记/云计算基础
笔记·学习·云计算
小老鼠爱大米4 小时前
C# WPF - Prism 学习篇:搭建项目(一)
c#·wpf·prism
wuxuanok7 小时前
Web后端开发-分层解耦
java·笔记·后端·学习
wuxuanok7 小时前
Web后端开发-请求响应
java·开发语言·笔记·学习
i7i8i9com8 小时前
后端微服务基础架构Spring Cloud
学习
蜡笔小电芯8 小时前
【C语言】指针与回调机制学习笔记
c语言·笔记·学习
博睿谷IT99_9 小时前
Hadoop 分布式存储与计算框架详解
wpf
im_AMBER9 小时前
学习日志03 python
学习