一:样式基础

如果我们的程序有三个这样的按键,一般我们会这样写

    <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的更多相关文章

  1. Bootstrap WPF Style,Bootstrap风格的WPF样式

    简介 GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style 此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CS ...

  2. C#工具:Bootstrap WPF Style,Bootstrap风格的WPF样式

    简介 GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style 此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CS ...

  3. WPF样式(Style)入门

    原文:WPF样式(Style)入门 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_34802416/article/details/78231 ...

  4. wpf 中关于Image中样式Style的一点总结

    第一种写法: (1):定义样式 <Style x:Key="imgStyle" TargetType="Image">  : <!-- Tar ...

  5. 第十四章:样式(Style)和主题(Theme)

    简介 Android的样式(Style)和主题(Theme)文件就好比WEB开发中的CSS一样,可以实现UI界面的风格统一管理,这和Windows平台的XAML格式(Silverlight.WPF)类 ...

  6. WPF 样式和行为

    样式(style):组织和重用格式化选项的重要工具,将细节如边距.字体.字号等信息封装起来,然后再需要的地方通过属性来应用样式. 行为(behavior):封装一些通用的UI行为,如拖动,缩放元素的代 ...

  7. WPF 之 style文件的引用

    总结一下WPF中Style样式的引用方法. 一.内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属 ...

  8. 写自己的WPF样式 - 窗体

    初试WPF样式,感觉还不错.上篇写完了按钮的样式下面写窗体,废话不多说直接上代码: (1)定义一个窗体样式"MyWpfWindow" <Style x:Key="M ...

  9. WPF的Style的TargetType不同写法的异同

    原文:WPF的Style的TargetType不同写法的异同 <Style TargetType="TextBlock"> <Setter Property=&q ...

随机推荐

  1. 阶段3 2.Spring_10.Spring中事务控制_5 spring事务控制的代码准备

    创建一个工程,只搭建环境不做配置.等配置的时候把这个项目相关的代码再复制到新项目里面 jar包的打包方式 导入包 事务控制也是基于AOP的.所以这里导入aspectjweaver 复制jdbcTemp ...

  2. 阶段3 2.Spring_03.Spring的 IOC 和 DI_9 spring的依赖注入

    新建工程 改成jar包 加入spring的依赖 复制之前的工程代码 再复制配置文件 fac factory整个删除 构造函数也删除.删除后的代码.如下 配置文件中的注释都删除掉 spring中的依赖注 ...

  3. 阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类

    如何找到接口的实现类 BeanFactory是核心容器的顶层接口 查看接口的实现类 接下来介绍这三个实现类 把bean.xml复制到桌面上面 运行测试程序 实际更常用ClassPathXmlAppli ...

  4. RedHat 5下安装gcc编译环境的具体步骤

    RedHat 5下安装gcc编译环境的具体步骤 在RHEL5系统中默认不安装linux系统中的开发编译环境(gcc),此软件包安装时依赖其他包较多 在以前使用RHEL4时可以通过如下命令安装: rpm ...

  5. mxml 嵌入as代码出错,缺少 CDATA

    如果<mx:Script> 中有大于小于符号,代码必须包含在<![CDATA[ 之中,否则会报错

  6. Java servlet和JSP的区别和联系

    Java servlet技术:在Java代码中嵌入HTML JSP技术:HTML输出时比较便捷,就在HTML中嵌入Java代码 Java servlet技术:擅长编写Java代码 JSP技术:擅长页面 ...

  7. ffi模块——node调用动态链接库(.so/.dll文件)

    参考资料1:[https://www.npmjs.com/package/ffi] 参考资料2:[http://imweb.io/topic/57732fbef0a5487b05f325bf] 参考资 ...

  8. beego框架学习(二) -路由设置

    路由设置 什么是路由设置呢?前面介绍的 MVC 结构执行时,介绍过 beego 存在三种方式的路由:固定路由.正则路由.自动路由,接下来详细的讲解如何使用这三种路由. 基础路由 从beego1.2版本 ...

  9. django设置mysql为数据库笔记

    1,guest/settings.py中加上 import pymysql pymysql.install_as_MySQLdb() 安装好pymysql 2,guest/settings.py的DA ...

  10. cocos2dx基础篇(26) 单例模式

    单例模式,说的通俗一点就是:创建某个类的全局唯一静态实例对象.也就是说从它创建开始,一直到整个游戏程序结束才会释放资源,期间一直保存的着数据. 单例类在大部分游戏中应该是必不可少的部分,如整个游戏音乐 ...