转载于:https://www.cnblogs.com/huangxincheng/category/388852.html

这个楼主写的很详解,也比较基础,刚学wpf的朋友看看很有帮助。

说起样式,大家第一反应肯定是css,好的,先上一段代码。

 html{border:;}
ul,form{margin:; padding:}
body,div,th,td,li,dd,span,p,a{font-size:12px; font-family:Verdana,Arial,"宋体";color:#;}
h3,input{font-size:12px; font-family:Verdana,Arial,"宋体";color:#4465a2;} body {
/*background-color:#eaeaea;*/
/*e5e5e5*/
/*BACKGROUND: url(../images/header_bg.jpg) #fff repeat-x;*/
BACKGROUND: url(../images/color_1.png) #fff repeat-x 0px -233px;
margin:0px;
padding:0px;
} ul{list-style:none;}
h1,h2,h4,h5,h6{ font-size:14px; color:#;}
img{border:;}
a {color:#;text-decoration:none;}
a:hover{color:#ff0000;text-decoration:underline;}

我们知道css实现了内容与样式的分离,既然wpf跟webform非常类似,那么肯定也有一套能够实现css的功能,是的。这就是wpf的style。

一:Style类

首先我们看看Style里面有哪些东西,在vs里面我们可以通过按F12查看类的定义。

下面我们一一解读下:

1:Setters

从上图我们知道Setters的类型是SetterBaseCollection,可以看得出是一个存放SetterBase的集合,SetterBase派生出了两个类

Setter和EventSetter,下面我们看看Setter类的定义。

这里我们看到了两个非常重要KV属性Property和Value,我们拿css找找对应关系。

html{border:0;}
html    => Style.TargetType

border =>   Property

0        =>   Value

估计大家想迫不及待的试一试,好了,我先做一个简单的demo。

 <Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Pink"/>
<Setter Property="FontSize" Value=""/>
</Style>
</Window.Resources>
<Grid>
<Button Content="一线码农"/>
</Grid>
</Window>

仔细看看,是不是找到了css的感觉,有人肯定要问,这不就是标签选择器吗?能不能做成“id选择器”,当然可以,我们只需要给style取一个名字,

然后在控件上引用一下就ok了。

 <Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style x:Key="mystyle" TargetType="Button">
<Setter Property="Background" Value="Pink"/>
<Setter Property="FontSize" Value=""/>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource ResourceKey=mystyle}" Content="一线码农"/>
</Grid>
</Window>

现在我们添加一个label,如果我们也需要同样的“背景色”和“字体”,那么我们是否要重新写一个label的样式吗?答案肯定是否定的,聪明的你肯定会

想到”基类“。我们发现label和button都是继承自ContentControl,都属于内容控件,那么何不在TargetType中定义为ContentControl不就ok了吗?

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style x:Key="mystyle" TargetType="ContentControl">
<Setter Property="Background" Value="Pink"/>
<Setter Property="FontSize" Value=""/>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource ResourceKey=mystyle}"
Content="Button" Height="" Margin="132,99,0,0" Name="button1" Width="" />
<Label Style="{StaticResource ResourceKey=mystyle}"
Content="Label" Height="" Margin="140,168,0,0" Name="label1" />
</Grid>
</Window>

2:TargetType

我们在说Setter的时候也提到了,其实TargetType也就是将样式施加到某一个对象上,具体的也没什么好说的。

3:BaseOn

我们知道css具有“继承和覆盖”的特性,同样我们的wpf中也是具有的。

<1>:继承

 <Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style x:Key="baseStyle" TargetType="Button">
<Setter Property="FontSize" Value=""/>
</Style>
<Style x:Key="childStyle" TargetType="Button"
BasedOn="{StaticResource ResourceKey=baseStyle}">
<Setter Property="Background" Value="Pink"/>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource ResourceKey=childStyle}" Content="一线码农"/>
</Grid>
</Window>

从上例中,我们看到childStyle继承到了baseStyle中的fontSize,最终的效果也是我们期望看到的。

<2>:覆盖

我们知道css遵循“就近原则”。

①:“行内”覆盖“嵌入”,“嵌入”覆盖“外部”

我们可以清楚的看到,行内样式覆盖了嵌入样式。

②:同级别遵循“就近”。

从button的颜色上看,我们可以获知Pink已经被BurlyWood覆盖。

4:Triggers

顾名思义,是触发器的意思,我们可以认为是wpf在style中注入了一些很简单,很sb的js代码。

wpf中有5种trigger,都是继承自TriggerBase类。

<1> Trigger,MuliTrigger

我们知道js是事件驱动机制的,比如触发mouseover,mouseout,click等事件来满足我们要处理的逻辑,那么wpf在不用写C#代码的情况下

用trigger就能够简单的满足这些事件处理。

下面举个例子

 <Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style x:Key="childStyle" TargetType="Button">
<Setter Property="Background" Value="BurlyWood"/>
<Style.Triggers>
<!-- 当IsMouseOver的时候,Button颜色变成粉色 -->
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Pink"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource ResourceKey=childStyle}" Content="一线码农">
</Button>
</Grid>
</Window>

最后的效果就是当isMouseOver=true的情况下,button的Background变成Pink。

然而trigger只能满足在单一的条件下触发,那么我想在多个条件同时满足的情况下才能触发有没有办法做到呢?刚好MuliTrigger就可以帮你实现。

 <Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style x:Key="childStyle" TargetType="Button">
<Setter Property="Background" Value="BurlyWood"/>
<Style.Triggers>
  <MultiTrigger>
    <MultiTrigger.Conditions>
      <Condition Property="IsMouseOver" Value="True"></Condition>
      <Condition Property="IsPressed" Value="True"></Condition>
    </MultiTrigger.Conditions>
    <Setter Property="Background" Value="Pink"/> 上面两个条件都成立时,才会触发这个设置
  </MultiTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource ResourceKey=childStyle}" Content="一线码农">
</Button>
</Grid>
</Window>

这里我们看到,只有满足了IsMouseOver和IsPressed的时候,我们的button才会变成粉色。

<2>DataTrigger,MultiDataTrigger

这个跟上面的Trigger有什么不同呢?其实也就是DataTrigger多了一个Binding的属性,当然它的实际应用也是最广泛的。

 <Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<Style x:Key="childStyle" TargetType="Control">
<Setter Property="Background" Value="BurlyWood"/>
<Style.Triggers>
<!-- 绑定当前的radio单选框,如果按钮选中,触发字体设置 -->
<DataTrigger Binding="{Binding ElementName=radio, Path=IsChecked}" Value="True">
<Setter Property="FontSize" Value=""/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<RadioButton Style="{StaticResource ResourceKey=childStyle}"
Name="radio" Content="我要变成20号字"></RadioButton>
</Grid>
</Window>

效果:

=>

当我们选中radio的时候,字体变大,同样MultiDataTrigger这个多条件的使用道理也是一样的,这里就不介绍了。

<3>EventTrigger

这个trigger与动画有关,目前项目中还没接触到,留给大家自己研究研究。

5:IsSealed

用于标记style是只读的,类似我们在C#中的Seal关键字,来达到不允许让继承类使用,wpf使用seal常常在C#代码里面控制,在xaml中我们

是找不到的,有兴趣的话,大家自己研究研究。

入门wpf—— 3、样式的更多相关文章

  1. 8天入门wpf(转)

    8天入门wpf—— 第一天 基础概念介绍 8天入门wpf—— 第二天 xaml详解 8天入门wpf—— 第三天 样式 8天入门wpf—— 第四天 模板 8天入门wpf—— 第五天 数据绑定 8天入门w ...

  2. 力挺8天入门wpf【转载】

    8天入门wpf—— 第八天 最后的补充 摘要: 从这一篇往前看,其实wpf中还有很多东西没有讲到,不过我的原则还是将比较常用的知识点过一遍,如果大家熟悉了这些知识,基本功也就打的差不多了,后续可以等待 ...

  3. 求助 WPF ListViewItem样式问题

    求助 WPF ListViewItem样式问题 .NET 开发 > Windows Presentation Foundation Вопрос 0 Нужно войти <Style ...

  4. WPF GroupBox 样式分享

    原文:WPF GroupBox 样式分享 默认样式 GroupBox 样式分享" title="WPF GroupBox 样式分享"> 添加样式后 GroupBox ...

  5. WPF DataGrid 样式设置

    隔行换色,鼠标单击,悬浮样式都有,其具体效果如图 1 所示. 图 1 WPF DataGrid 样式设置效果图 其中: 界面设计代码下所示 ? + 查看代码 1 2 3 4 5 6 7 8 9 10 ...

  6. css入门二-常用样式

    css入门二-常用样式总结 基本标签样式 背景色background-color 高度height; 宽度width; 边框对齐以及详细设定举例 width/*宽度*/: 80%; height/*高 ...

  7. WPF DataGrid 样式分享

    原文:WPF DataGrid 样式分享 隔行换色,鼠标单击,悬浮样式都有 先看效果: 代码: <DataGrid AutoGenerateColumns="False" N ...

  8. wpf 导出Excel Wpf Button 样式 wpf简单进度条 List泛型集合对象排序 C#集合

    wpf 导出Excel   1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 4 ExportDataGrid ...

  9. 自定义WPF 窗口样式

    原文:自定义WPF 窗口样式 Normal 0 false 7.8 pt 0 2 false false false EN-US ZH-CN X-NONE 自定义 Window 在客户端程序中,经常需 ...

  10. WPF中样式和行为和触发器

    原文:WPF中样式和行为和触发器 样式简介:样式(style)是组织和重用格式化选项的重要工具,不是使用重复的标记填充XAML,以便设置外边距.内边距.颜色以及字体等细节.而是创建一系列封装所有这些细 ...

随机推荐

  1. python 启动pydoc查看文档

    启动pydoc查看文档 python3 -m pydoc -p 访问http://localhost:6789 或者查看官方文档:https://seleniumhq.github.io/seleni ...

  2. AttributeError: module 'pytest' has no attribute 'allure'

    解决 pip3 uninstall pytest-allure-adaptor pip3 install allure-pytest 参考: https://www.cnblogs.com/lansa ...

  3. UDF——定制窗口

    获取实例句柄的代码来自:https://blog.csdn.net/xie1xiao1jun/article/details/22180815 在Fluent当中我们可以使用scheme来为Fluen ...

  4. Fluent设置充分发展湍流入口(利用profile)

    计算模型: 物性参数为: 密度:100kg/m3 粘度系数:0.003333kg/(m·s) 原视频下载地址: https://pan.baidu.com/s/1W3n_K-dZCVMF7M63wV2 ...

  5. webpack vue-cli2 配置打包测试环境

    目前vue-cli2上原配置是只有开发环境dev和线上环境prod的配置,但是我们实际场景上还有很多需要一个测试环境test,下面就是对测试环境的配置,将测试环境和线上环境的打包代码分开就不需要切来切 ...

  6. webpack4.0中文文档踩坑记录

    一直没有正儿八经去看过webpack4.0的文档,前段时间工作比较轻松,于是就有了此文...面都这样一个问题:请问在您的开发生涯中,令你最痛苦最无奈的是什么?小生的回答只有一个:“阅读那些令人发指的文 ...

  7. KAFKA && zookeeper 集群安装

    服务器:#vim /etc/hosts10.16.166.90 sh-xxx-xxx-xxx-online-0110.16.168.220 sh-xx-xxx-xxx-online-0210.16.1 ...

  8. webssocket简介-服务器可以主动传送数据给浏览器

    WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客户端推送数据.在WebSocket API中,浏览器和服务器只需要完成一次握手,两者之间就直接可以创建持久性的连接,并 ...

  9. 【C++】C++中的类模板

    基础的类模板 模板类的继承 内部声明定义普通模板函数和友元模板函数 内部声明友元模板函数+外部定义友元模板函数 声明和定义分别在不同的文件(模板函数.模板友元) C++中有一个重要特性,那就是模板类型 ...

  10. (转)Ngx_Lua使用分享

    原文:https://www.cnblogs.com/yanzi-meng/p/9450999.html ngx_lua 模块详细讲解(基于openresty)---https://www.cnblo ...