欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > 记录 | WPF基础学习Style局部和全局调用

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

2025/2/8 9:46:05 来源:https://blog.csdn.net/qq_41714549/article/details/145471893  浏览:    关键词:记录 | 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之间区别。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com