WPF属性---重复样式和触发器

简介: WPF属性---重复样式和触发器

重复样式


<StackPanel>


<Button FontSize="20" Foreground="Red" Content="hello" Width="100" Height="40"/>


<Button FontSize="20" Foreground="Red" Content="hello" Width="100" Height="40"/>


<Button FontSize="20" Foreground="Red" Content="hello" Width="100" Height="40"/>


</StackPanel>


68da6aa57df1f9eb2efdaa5ac54282bd_b1c5bf9542adda0ba84e62e54034db74.png


通过以上的设置我们发现有很多重复的代码,在编写代码的时候,我们要避免代码的重复,代码的冗余,我们需要借助window资源,<window.Resources>


都要设置一个键值,通过键值找到样式X:key,例如:X:key=“defaultStyle”,通过defaultStyle找到样式


设置类型,TargetType。我们要为button设置样式,那么TargetType的类型就是button:TargetType=“button”。在接下来的设置中,我们就可以依据button的属性来设置


ba25ac83b4a3ec31b4be9411d7d69fbb_3dda0de02c40cc7529e683d736ce13f1.png


<Window.Resources>


<Style x:Key="defaultStyle" TargetType="Button"><!--TargetType要为谁设置样式-->


<Setter Property="FontSize" Value=" 30"/>


<Setter Property="Foreground" Value="blue"/>


<Setter Property="Width" Value="10"/>


</Style>


</Window.Resources>


<Grid>


<StackPanel>


按钮调用


<Button Style="{StaticResource defaultStyle}" Foreground="Red" Content="hello" Width="100" Height="40"/>


<Button FontSize="20" Foreground="Red" Content="hello" Width="100" Height="40"/>


<Button FontSize="20" Foreground="Red" Content="hello" Width="100" Height="40"/>


</StackPanel>


</Grid>


触发器


触发器就像开关一样,比如说一个按钮放上去的时候是什么样的拿下来又是什么样的


<Style.Triggers>


Trigger 为其中的一个触发器,还有其他样式


<Trigger Property="IsMouseOver" Value="True">


设置的各个属性


<Setter Property="Foreground" Value="Red"/>


<Setter Property="FontSize" Value="100"/>


</Trigger>


</Style.Triggers>


表现效果,当鼠标移动到按钮上会发生字体变为共色,字体为100


多条件触发器


<Style.Triggers>


<MultiTrigger><!--多条件触发器 当满足多个条件之后才触发的-->


<MultiTrigger.Conditions>


<!--两个条件同时满足--> 相当于这里是if语句


<Condition Property="IsMouseOver" Value="True"/>


<Condition Property=" IsFocused" Value="True"/>


</MultiTrigger.Conditions>


<!--满足以上两个条件执行什么-->就执行什么语句


<MultiTrigger.Setters>


<Setter Property="Foreground" Value="Red"/>


</MultiTrigger.Setters>


</MultiTrigger>


</Style.Triggers>


事件触发器


<EventTrigger RoutedEvent="Mouse.MouseEnter">


<EventTrigger.Actions>


<BeginStoryboard><!--动画效果-->


<Storyboard>


<DoubleAnimation Duration="0:0:0.2"


Storyboard.TargetProperty="FontSize" To="30">


</DoubleAnimation>


</Storyboard>


</BeginStoryboard>


</EventTrigger.Actions>


</EventTrigger>


相关文章
|
8月前
|
C#
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
152 0
|
2月前
|
C#
浅谈WPF之样式与资源
WPF通过样式,不仅可以方便的设置控件元素的展示方式,给用户呈现多样化的体验,还简化配置,避免重复设置元素的属性,以达到节约成本,提高工作效率的目的,样式也是资源的一种表现形式。本文以一个简单的小例子,简述如何设置WPF的样式以及资源的应用,仅供学习分享使用,如有不足之处,还请指正。
43 0
|
8月前
WPF-布局样式练习-Day02-聊天气泡
WPF-布局样式练习-Day02-聊天气泡
129 1
|
6月前
|
C#
2000条你应知的WPF小姿势 基础篇<57-62 依赖属性进阶>
2000条你应知的WPF小姿势 基础篇<57-62 依赖属性进阶>
20 0
|
6月前
|
存储 开发框架 .NET
2000条你应知的WPF小姿势 基础篇<51-56 依赖属性>
2000条你应知的WPF小姿势 基础篇<51-56 依赖属性>
22 0
|
8月前
|
C#
WPF-Binding问题-模板样式使用Binding TemplatedParent与TemplateBinding区别
WPF-Binding问题-模板样式使用Binding TemplatedParent与TemplateBinding区别
82 0
|
8月前
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
114 0
|
8月前
|
C#
WPF-布局样式练习-Day01
WPF-布局样式练习-Day01
71 0
|
8月前
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
72 0
|
前端开发 C#
wpf引用样式
wpf引用样式
93 0