当前位置: 首页 > news >正文

二、WPF——Style样式玩法(通过资源字典将Style独立,全局调用)

该博文参考B站博主丑萌气质狗视频学习小笔记,如有侵权联系立删
视频链接:WPF入门

第一篇博文链接:一、WPF入门介绍+Grid和StackPanel布局介绍+实战模拟Notepad++页面布局

接着第一篇博文继续哈,添加一个窗口
在这里插入图片描述
在这里插入图片描述
App.xaml中修改启动页面为StartupUri="Btn_Style.xaml"

一、给Button更换风格

首先我们使用了一个StackPanel布局,默认是水平布局(Orientation="Vertical"),每个button都可以设置很多属性,比如宽高、字体大小、背景颜色等
在这里插入图片描述

<Window x:Class="WpfApp1.Btn_Style"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:WpfApp1"mc:Ignorable="d"Title="Btn_Style" Height="450" Width="800"><StackPanel><Button Content="登录" Height="50" Width="100" FontSize="20" Background="Green"/><Button Content="退出" Height="50" Width="100" FontSize="20" Background="Green"/></StackPanel>
</Window>

二、提取Button共有Style,其他Button直接延用

上面案例我们可以看到,两个button其实是一样的,那我们就没必要去重复代码了

我们可以通过Window.Resources里面的Style TargetType="Button"来绑定Button控件
通过<Setter Property="Height" Value="50"/>开抽离公共属性,后续所有的Button控件都可以使用
在这里插入图片描述

<Window x:Class="WpfApp1.Btn_Style"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:WpfApp1"mc:Ignorable="d"Title="Btn_Style" Height="450" Width="800"><Window.Resources><Style TargetType="Button"><Setter Property="Height" Value="50"/><Setter Property="Width" Value="100"/><Setter Property="FontSize" Value="20"/><Setter Property="Background" Value="Green"/></Style></Window.Resources><StackPanel><Button Content="登录"/><Button Content="退出"/></StackPanel></Window>

三、多个Style,指定哪个,应用哪个

我们可以看到,所有的Button都一样了,但是我们的现实需求是,需要用到的时候再用该style,不需要的时候保持默认样式
我们可以通过Style x:Key="Login" TargetType="Button"中的x:Key来指定关键字,例如是Login
后面的Button可以通过<Button Content="登录" Style="{StaticResource Login}"/>中的StaticResource Login进行调用即可
在这里插入图片描述

<Window x:Class="WpfApp1.Btn_Style"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:WpfApp1"mc:Ignorable="d"Title="Btn_Style" Height="450" Width="800"><Window.Resources><Style x:Key="Login" TargetType="Button"><Setter Property="Height" Value="70"/><Setter Property="Width" Value="120"/><Setter Property="FontSize" Value="20"/><Setter Property="Background" Value="Green"/></Style><Style x:Key="Quit" TargetType="Button"><Setter Property="Height" Value="50"/><Setter Property="Width" Value="100"/><Setter Property="FontSize" Value="20"/><Setter Property="Background" Value="Red"/></Style></Window.Resources><StackPanel Orientation="Vertical"><Button Content="登录" Style="{StaticResource Login}"/><Button Content="退出" Style="{StaticResource Quit}"/></StackPanel></Window>

四、多个Style,指定哪个,应用哪个,若不指定则使用BaseStyle

我们需要将BaseStyle表示默认的Button风格,其他的Style可以继承此,方便后续进行操作,特例化
<Style TargetType="Button">定义一个应用于所有Button的风格

<Style x:Key="Login" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">表明该Style是基于x:Type Button的原先Button控件,后续的可以覆盖前面的

之前是白色的,后面的<Style x:Key="Login" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Style x:Key="Quit" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">均继承了前面的风格,但若有重复的则会覆盖掉,这里以Background进行覆盖演示
在这里插入图片描述

<Window x:Class="WpfApp1.Btn_Style"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:WpfApp1"mc:Ignorable="d"Title="Btn_Style" Height="450" Width="800"><Window.Resources><Style TargetType="Button"><Setter Property="Height" Value="70"/><Setter Property="Width" Value="120"/><Setter Property="FontSize" Value="20"/><Setter Property="Background" Value="White"/></Style><Style x:Key="Login" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Background" Value="Green"/></Style><Style x:Key="Quit" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Background" Value="Red"/></Style></Window.Resources><StackPanel Orientation="Vertical"><Button Content="登录" Style="{StaticResource Login}"/><Button Content="退出" Style="{StaticResource Quit}"/><Button Content="test"/></StackPanel></Window>

五、使用资源字典将所有的Style独立到其他XAML文件中,通过外部调用

1,新建资源字典,用于存放所有的Style格式

在这里插入图片描述
在这里插入图片描述

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Style TargetType="Button"><Setter Property="Height" Value="70"/><Setter Property="Width" Value="120"/><Setter Property="FontSize" Value="20"/><Setter Property="Background" Value="White"/></Style><Style x:Key="Login" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Content" Value="Login"/><Setter Property="Background" Value="Green"/></Style><Style x:Key="Quit" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Content" Value="Quit"/><Setter Property="Background" Value="Red"/></Style>
</ResourceDictionary>

2,在App.xaml中引入该资源字典

在这里插入图片描述
把资源字典(BaseButtonDictionary.xaml)的相对路径填入即可

<Application x:Class="WpfApp1.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp1"StartupUri="Btn_Style.xaml"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/BaseButtonDictionary.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
</Application>

3,在其他窗口中直接使用

在这里插入图片描述
在这里插入图片描述
使用一个2列的Grid表格布局,每一列都占1/2
每一列都放一个button,一个是Login风格,一个是Quit风格

需要在App.xaml中修改启动窗口StartupUri="test.xaml"
在这里插入图片描述

<Window x:Class="WpfApp1.test"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:WpfApp1"mc:Ignorable="d"Title="test" Height="450" Width="800"><Grid Grid.Row="2"><Grid.ColumnDefinitions><ColumnDefinition Width="1*"/><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><Button Style="{StaticResource Login}" Grid.Column="0"/><Button Style="{StaticResource Quit}"  Grid.Column="1"/></Grid>
</Window>

4,完整代码

①App.xaml

<Application x:Class="WpfApp1.App"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="clr-namespace:WpfApp1"StartupUri="test.xaml"><Application.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="/BaseButtonDictionary.xaml"/></ResourceDictionary.MergedDictionaries></ResourceDictionary></Application.Resources>
</Application>

②BaseButtonDictionary.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Style TargetType="Button"><Setter Property="Height" Value="70"/><Setter Property="Width" Value="120"/><Setter Property="FontSize" Value="20"/><Setter Property="Background" Value="White"/></Style><Style x:Key="Login" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Content" Value="Login"/><Setter Property="Background" Value="Green"/></Style><Style x:Key="Quit" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"><Setter Property="Content" Value="Quit"/><Setter Property="Background" Value="Red"/></Style>
</ResourceDictionary>

③test.xaml

<Window x:Class="WpfApp1.test"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:WpfApp1"mc:Ignorable="d"Title="test" Height="450" Width="800"><Grid Grid.Row="2"><Grid.ColumnDefinitions><ColumnDefinition Width="1*"/><ColumnDefinition Width="1*"/></Grid.ColumnDefinitions><Button Style="{StaticResource Login}" Grid.Column="0"/><Button Style="{StaticResource Quit}"  Grid.Column="1"/></Grid>
</Window>

效果展示:
在这里插入图片描述


文章转载自:

http://P7onrQ6z.fkrzx.cn
http://abJL8neA.fkrzx.cn
http://mc2CFYy2.fkrzx.cn
http://wnZf3CIl.fkrzx.cn
http://3gGoRHfC.fkrzx.cn
http://du5QLEUk.fkrzx.cn
http://G9ccHJyp.fkrzx.cn
http://lx6oSowy.fkrzx.cn
http://ztzqoiqU.fkrzx.cn
http://VN09cxTc.fkrzx.cn
http://Hpw7VPrD.fkrzx.cn
http://nKpUMPvC.fkrzx.cn
http://JDqf65xG.fkrzx.cn
http://Bz7pJw22.fkrzx.cn
http://Hk3cSHfr.fkrzx.cn
http://aQ9cIz8W.fkrzx.cn
http://B2tdXe53.fkrzx.cn
http://wYhfqT2R.fkrzx.cn
http://O8WeMdv6.fkrzx.cn
http://SmZ9SzJs.fkrzx.cn
http://5vVAGBwS.fkrzx.cn
http://KjL23UPY.fkrzx.cn
http://sJ7vDWkR.fkrzx.cn
http://ropnQfFl.fkrzx.cn
http://2Q2BB8Kx.fkrzx.cn
http://HcD7PFnN.fkrzx.cn
http://apzZpf9s.fkrzx.cn
http://N5VF7sbw.fkrzx.cn
http://KNWSsTll.fkrzx.cn
http://tR3Pwo1r.fkrzx.cn
http://www.dtcms.com/a/378862.html

相关文章:

  • 基于Hadoop进程的分布式计算任务调度与优化实践——深入理解分布式计算引擎的核心机制
  • 用工招聘小程序:功能版块与前端设计解析
  • Golang高效JSON处理:easyjson性能提升6倍
  • Golang语言入门之数组、切片与子切片
  • Go 死锁全解析:4个条件+5个场景+6个解决方案
  • Go语言快速入门教程(JAVA转go)——1 概述
  • 【leetcode】139. 单词拆分
  • 使用yocto工具链交叉编译lsof命令
  • vue项目的main.js规划设计与合理使用
  • FPGA入门-无源蜂鸣器驱动
  • 使用Langchain生成本地rag知识库并搭载大模型
  • [第一章] web入门—N1book靶场详细思路讲解
  • uniapp 文件查找失败:main.js
  • 第7篇、Kafka Streams 与 Connect:企业级实时数据处理架构实践指南
  • Linux redis 8.2.1源码编译
  • logging 模块升级版 loguru
  • 【Flask】实现一个前后端一体的项目-脚手架
  • 小说阅读系统Java源码 小说阅读软件开发 小说app小程序
  • 如何在 Debian 12 上安装 MySQL
  • GA-PNN: 基于遗传算法的光子神经网络硬件配置方法(未做完)
  • STM32基础篇--GPIO
  • 无人机遥控器射频模块技术解析
  • Docker 命令核心语法
  • 第五章:Python 数据结构:列表、元组与字典(一)
  • Python快速入门专业版(二十一):if语句基础:单分支、双分支与多分支(判断用户权限案例)
  • 学习笔记:JavaScript(4)——DOM节点
  • 软考中级习题与解答——第四章_软件工程(3)
  • 消息队列-kafka完结
  • SKywalking Agent配置+Oracle监控插件安装指南
  • Skywalking告警配置+简易邮件告警应用配置(保姆级)