wpf的样式可以把属性一样的设置写在样式里面,这样可以减少代码量。

举个简单的例子:

<Window x:Class="WpfPictureClick.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:WpfPictureClick"xmlns:sys="clr-namespace:System;assembly=mscorlib"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><Style x:Key="BigFrontStyle"><Setter Property="Control.FontFamily" Value="Time New Roman"></Setter><Setter Property="Control.FontSize" Value="18"></Setter></Style></Window.Resources><Grid><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Button Grid.Row="0" Style="{StaticResource BigFrontStyle}" Width="80" Height="80" Content="3333"></Button><Button Grid.Row="1"  Width="80" Height="80" Content="3333"></Button></Grid>
</Window>

样式需要在Window.Resources里面定义,然后在下面使用,直接上结果的图片

明显字体是不一样的,这个时候,样式的作用就体现出来了

*****************************除了设置属性之后,样式还能够设置事件*************

<Window x:Class="WpfPictureClick.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:WpfPictureClick"xmlns:sys="clr-namespace:System;assembly=mscorlib"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><Style x:Key="EventStyle"><EventSetter Event="Button.MouseEnter" Handler="FrameworkElement_MouseEnter"></EventSetter><EventSetter Event="Button.MouseLeave" Handler="FrameworkElement_MouseLeave"></EventSetter></Style></Window.Resources><Grid><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Button Grid.Row="0" Style="{StaticResource EventStyle}" Width="80" Height="80" Content="3333"></Button><Button Grid.Row="1"  Width="80" Height="80" Content="3333"></Button></Grid>
</Window>

通过EventSetter来设置

处理函数如下:

 public void FrameworkElement_MouseEnter(object sender, MouseEventArgs e){((Button)sender).Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x2c, 0x2c, 0x2c));}private void FrameworkElement_MouseLeave(object sender, MouseEventArgs e){((Button)sender).Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x12, 0x57, 0x90));}

结果如下:

*****************************除了设置事件之后,样式有个王牌,叫触发器*************

<Window x:Class="WpfPictureClick.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:WpfPictureClick"xmlns:sys="clr-namespace:System;assembly=mscorlib"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><Style x:Key="EventStyle"><EventSetter Event="Button.MouseEnter" Handler="FrameworkElement_MouseEnter"></EventSetter><EventSetter Event="Button.MouseLeave" Handler="FrameworkElement_MouseLeave"></EventSetter></Style><Style x:Key="TriggerStyle" ><Style.Triggers><Trigger Property="Button.IsMouseOver" Value="True"><Setter Property="Button.Background" Value="red"></Setter></Trigger><Trigger Property="Button.IsMouseOver" Value="false"><Setter Property="Button.Background" Value="Black"></Setter></Trigger></Style.Triggers></Style></Window.Resources><Grid><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Button Grid.Row="0" Focusable="False" Style="{StaticResource TriggerStyle}" Width="80" Height="80" Content="3333"></Button><Button Grid.Row="1" Width="80" Height="80" Content="3333"></Button></Grid>
</Window>

通过Style.Triggers来设置触发器,效果如下:

wpf之样式属性、事件、触发器相关推荐

  1. WPF系列教程(二十九):触发器Triggers、MultiTrggers、EventTrigger——属性触发器、多触发器、事件触发器

    使用触发器可以自动完成简单的样式改变. 项目源码 触发器 在Style定义时使用Style.Triggers属性来实现: <!--设置触发器--> <Style.Triggers&g ...

  2. transitionend、change、classList、兼容代码、元素样式属性的操作、-Attribute自定义属性、阻止跳转、元素绑定相同事件、元素解绑事件、事件冒泡、事件三阶段

    transitionend过渡监听事件: 过渡监听事件transitionend指的是CSS3中过渡效果执行一次后触发事件处理函数,如下案例: <!DOCTYPE html><htm ...

  3. vue、Cascader 级联选择、Cascader 属性事件方法、vue Cascader 所有级联选择样式、vue Cascader 级联选择全部属性事件方法

    vue.Cascader 级联选择.Cascader 属性事件方法.vue Cascader 所有级联选择样式.vue Cascader 级联选择全部属性事件方法 Cascader 级联选择 何时使用 ...

  4. WPF的依赖属性和附加属性(用法解释较全)

    转:https://www.cnblogs.com/zhili/p/WPFDependencyProperty.html 一.引言 感觉最近都颓废了,好久没有学习写博文了,出于负罪感,今天强烈逼迫自己 ...

  5. vue、Layout 布局、Layout 属性事件、vue Layout 全部布局、vue Layout 全部属性事件

    vue.Layout 布局.Layout 属性事件.vue Layout 全部布局.vue Layout 全部属性事件 设计规则 尺寸 交互 视觉 组件概述 代码演示 1.基本结构 2.自定义触发器 ...

  6. WPF的样式(Style)继承

    WPF的样式(Style)继承 参考自http://www.cnblogs.com/SkyD/archive/2008/08/09/1264294.html,非常感谢. 用Style的BaseOn属性 ...

  7. WPF中的鼠标事件详解

    WPF中的鼠标事件详解 Uielement和ContentElement都定义了十个以Mouse开头的事件,8个以PreviewMouse开头的事件,MouseMove,PreviewMouseMov ...

  8. WPF 学习笔记 路由事件

    1. 可传递的消息: WPF的UI是由布局组建和控件构成的树形结构,当这棵树上的某个节点激发出某个事件时,程序员可以选择以传统的直接事件模式让响应者来响应之,也可以让这个事件在UI组件树沿着一定的方向 ...

  9. 操作属性之修改样式属性

    操作属性之修改样式属性 <!DOCTYPE html> <html lang="en"><head><meta charset=" ...

最新文章

  1. 连接Oracle错误:800a0e7a未找到提供程序的解决
  2. linux 如何禁用账号和解除禁用账号
  3. 配置腾讯云服务器-2021-3-27
  4. JavaMelody+Spring+struts2配置详解——系统监控
  5. Node.js静态文件服务器实战[转]
  6. mysql 视图 教程_MySQL VIEW(视图)
  7. Android Studio的Gradle常用命令配置和依赖管理
  8. Delphi工具之Image Editor
  9. XP无法建立宽带连接的解决方法
  10. 互斥锁、死锁、递归锁、信号量、Event
  11. Docker安装Redis(docker-compose.yml)
  12. 开源代码准确率99%+,人脸识别问题真的被解决了吗?
  13. ncurses屏幕操作:getyx(),getparyx(),getmaxyx(),scr_dump(),scr_restore(),getwin(),putwin()
  14. 如何用python和flask以太坊智能合约开发
  15. 【原创】ABAP根据文件路径获取文件所在目录
  16. 离散数学 第二类斯特林数 小白学习笔记
  17. 使用Excel背单词-高效-简单
  18. 布朗运动、伊藤引理、BS公式
  19. Python的三目表达式and简短语法
  20. gopher攻击mysql_从一道CTF题目看Gopher攻击MySql

热门文章

  1. 转 Linux查看文件编码格式及文件编码转换
  2. [Step By Step]SAP HANA PAL多元线性回归预测分析Linear Regression实例FORECASTWITHLR(预测)...
  3. [图形图像]一次光线追踪的尝试
  4. C#笔记20:多线程之线程同步中的信号量AutoResetEvent和ManualResetEvent
  5. spring整合redis问题
  6. EMNLP自然语言处理经验方法
  7. php后缀名隐藏,php隐藏后缀名的方法是什么
  8. Spring框架(IoC、AOP面向接口切面)
  9. 19电子设计速成实战宝典pdf_开发宝典丛书:Visual C++编程实战宝典PDF
  10. java基础知识总结(4)