WPF 样式Style
一:样式基础

如果我们的程序有三个这样的按键,一般我们会这样写
<StackPanel>
<!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为16-->
<Button Content="Button1" Background="Azure" Foreground="Coral" FontFamily="Arial" FontWeight="Bold" FontSize="16" />
<Button Content="Button2" Background="Azure" Foreground="Coral" FontFamily="Arial" FontWeight="Bold" FontSize="16" />
<Button Content="Button3" Background="Azure" Foreground="Coral" FontFamily="Arial" FontWeight="Bold" FontSize="16" />
</StackPanel>
但是如果我们的程序有很多这样的按键,每一个都设置一遍外观属性代码就会显得很冗余,有好奇心的小伙伴就会想有没有一种办法让代码变得简洁一些?答案是:Style
<Window.Resources>
<Style x:Key="ButtonStyle">
<!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为16-->
<Setter Property="Control.FontFamily" Value="Arial"></Setter>
<Setter Property="Control.Background" Value="Azure"></Setter>
<Setter Property="Control.Foreground" Value="Coral"></Setter>
<Setter Property="Control.FontWeight" Value="Bold"></Setter>
<Setter Property="Control.FontSize" Value="16"></Setter>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="Button1" Style="{StaticResource ButtonStyle}" />
<Button Content="Button2" Style="{StaticResource ButtonStyle}" />
<Button Content="Button3" Style="{StaticResource ButtonStyle}" />
</StackPanel>
这样代码就会显得简洁一些,细心的小伙伴儿发现所有的按键都用Style="{StaticResource ButtonStyle}"来指定样式,感觉还是略有一点冗余,那我们还可以继续让代码简洁一些,把
<Style x:Key="ButtonStyle">样式里的键值换成目标类型TargetTpye="Button",
<Window.Resources>
<Style TargetType="Button">
<!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为16-->
<Setter Property="Control.FontFamily" Value="Arial"></Setter>
<Setter Property="Control.Background" Value="Azure"></Setter>
<Setter Property="Control.Foreground" Value="Coral"></Setter>
<Setter Property="Control.FontWeight" Value="Bold"></Setter>
<Setter Property="Control.FontSize" Value="16"></Setter>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="Button1" />
<Button Content="Button2" />
<Button Content="Button3" />
</StackPanel>
这样三个按键的代码就非常简洁了,但是有的小伙伴儿就想让第一个和第三个按键用上面的样式,第二个不用这样的样式,我们可以这样改
<Window.Resources>
<Style TargetType="Button">
<!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为16-->
<Setter Property="Control.FontFamily" Value="Arial"></Setter>
<Setter Property="Control.Background" Value="Azure"></Setter>
<Setter Property="Control.Foreground" Value="Coral"></Setter>
<Setter Property="Control.FontWeight" Value="Bold"></Setter>
<Setter Property="Control.FontSize" Value="16"></Setter>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="Button1" />
<Button Content="Button2" Style="{x:Null}" />
<Button Content="Button3" />
</StackPanel>
效果如下

二:样式的事件
当我们想让鼠标经过按键时,前景色变为蓝色,鼠标离开时,前景色变为珊瑚色

一般我们会这样写前端代码
<Window.Resources>
<Style TargetType="Button">
<!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为16-->
<Setter Property="Control.FontFamily" Value="Arial"></Setter>
<Setter Property="Control.Background" Value="Azure"></Setter>
<Setter Property="Control.Foreground" Value="Coral"></Setter>
<Setter Property="Control.FontWeight" Value="Bold"></Setter>
<Setter Property="Control.FontSize" Value="16"></Setter>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="Button1" MouseEnter="btnMouseEnter" MouseLeave="btnMouseLeave" />
<Button Content="Button2" MouseEnter="btnMouseEnter" MouseLeave="btnMouseLeave" />
<Button Content="Button3" MouseEnter="btnMouseEnter" MouseLeave="btnMouseLeave"/>
</StackPanel>
然后为后台代码添加事件处理事件
private void btnMouseEnter(object sender, MouseEventArgs e)
{
((Button)sender).Foreground = new SolidColorBrush(Colors.Blue);//字体颜色改为蓝色
} private void btnMouseLeave(object sender, MouseEventArgs e)
{
((Button)sender).Foreground = new SolidColorBrush(Colors.Coral);//字体颜色改为珊瑚色
}
这样每个按键都有一个鼠标进入事件和一个离开事件MouseEnter="btnMouseEnter" MouseLeave="btnMouseLeave"。聪明的小伙伴儿就会想样式可以简化控件的外观,那可以不可以简化控件的事件呢?答案是:EventSetter
<Window.Resources>
<Style TargetType="Button">
<!--按键的背景色为Azure蔚蓝色背景色为Coral珊瑚色字体为Arial加粗字体大小为16-->
<Setter Property="Control.FontFamily" Value="Arial"></Setter>
<Setter Property="Control.Background" Value="Azure"></Setter>
<Setter Property="Control.Foreground" Value="Coral"></Setter>
<Setter Property="Control.FontWeight" Value="Bold"></Setter>
<Setter Property="Control.FontSize" Value="16"></Setter>
<EventSetter Event="FrameworkElement.MouseEnter" Handler="btnMouseEnter"></EventSetter>
<EventSetter Event="FrameworkElement.MouseLeave" Handler="btnMouseLeave"></EventSetter>
</Style>
</Window.Resources>
<StackPanel>
<Button Content="Button1" />
<Button Content="Button2" />
<Button Content="Button3" />
</StackPanel>
<EventSetter Event="FrameworkElement.MouseEnter" Handler="btnMouseEnter"></EventSetter> EventSetter :设置样式的事件设置,Event:事件类型,Handler:事件处理程序btnMouseEnter就是我们刚才写的后台代码事件处理程序没有任何变化
private void btnMouseEnter(object sender, MouseEventArgs e)
{
((Button)sender).Foreground = new SolidColorBrush(Colors.Blue);//字体颜色改为蓝色
}
这样写代码就会很简洁,也便于维护。
WPF 样式Style的更多相关文章
- Bootstrap WPF Style,Bootstrap风格的WPF样式
简介 GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style 此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CS ...
- C#工具:Bootstrap WPF Style,Bootstrap风格的WPF样式
简介 GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style 此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CS ...
- WPF样式(Style)入门
原文:WPF样式(Style)入门 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_34802416/article/details/78231 ...
- wpf 中关于Image中样式Style的一点总结
第一种写法: (1):定义样式 <Style x:Key="imgStyle" TargetType="Image"> : <!-- Tar ...
- 第十四章:样式(Style)和主题(Theme)
简介 Android的样式(Style)和主题(Theme)文件就好比WEB开发中的CSS一样,可以实现UI界面的风格统一管理,这和Windows平台的XAML格式(Silverlight.WPF)类 ...
- WPF 样式和行为
样式(style):组织和重用格式化选项的重要工具,将细节如边距.字体.字号等信息封装起来,然后再需要的地方通过属性来应用样式. 行为(behavior):封装一些通用的UI行为,如拖动,缩放元素的代 ...
- WPF 之 style文件的引用
总结一下WPF中Style样式的引用方法. 一.内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属 ...
- 写自己的WPF样式 - 窗体
初试WPF样式,感觉还不错.上篇写完了按钮的样式下面写窗体,废话不多说直接上代码: (1)定义一个窗体样式"MyWpfWindow" <Style x:Key="M ...
- WPF的Style的TargetType不同写法的异同
原文:WPF的Style的TargetType不同写法的异同 <Style TargetType="TextBlock"> <Setter Property=&q ...
随机推荐
- flutter 快速生成Widget
快速生成对象 List.generate(20, (i){ return Text("$i"); }), 快速生成Widget ListView.builder( itemCoun ...
- Redis 入门 3.3 散列类型
3.3.1 介绍 散列类型(hash)的键值也是一种字典结构,其储存了字段(field)和字段值的映射,但字段值只能是字符串,不支持其他数据类型,换句话说,散列类型不能嵌套其他的数据类型.一个散列 ...
- 【PyTorch】计算局部相似矩阵
计算局部相似矩阵 代码文档:https://github.com/lartpang/mypython/blob/master/2019-09-25%E8%AE%A1%E7%AE%97%E5%B1%80 ...
- 零基础学习前端1-1配置node及npm环境变量
零基础学习前端1-1配置node及npm环境变量 ## 1-1配置node及npm环境变量 首先:下载node 可以直接去官方网站下载 1.首先从官网下载安装包 https://nodejs.org/ ...
- BUUOJ reverse 刮开有奖
刮开有奖 这是一个赌博程序,快去赚钱吧!!!!!!!!!!!!!!!!!!!!!!!!!!!(在编辑框中的输入值,即为flag,提交即可) 注意:得到的 flag 请包上 flag{} 提交 拖到id ...
- HDU 1160 FatMouse's Speed (动态规划、最长下降子序列)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- springboot - 应用实践(2)第一个springboot应用
1.使用maven创建一个快速启动项目 2.引入相关依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...
- shiro三连斩之概念
1, 什么是Shiro? Shiro是一个安全框架,用于解决系统的认证和授权问题,同时提供了会话管理,数据加密,与WEB集成,缓存等机制. Authentication:身份认证/登录,验证用户是不是 ...
- laravel修改用户模块的密码验证
做项目的时候,用户认证几乎是必不可少的,如果我们的项目由于一些原因不得不使用 users 之外的用户表进行认证,那么就需要多做一点工作来完成这个功能. 现在假设我们只需要修改登录用户的表,表名和表结构 ...
- 学会这些 pycharm 编程小技巧,编程效率提升 10 倍
PyCharm 是一款非常强大的编写 python 代码的工具.掌握一些小技巧能成倍的提升写代码的效率,本篇介绍几个经常使用的小技巧. 一.分屏展示 当你想同时看到多个文件的时候: 1.右击标签页: ...