WPF基础之样式
2021-06-21 19:03
标签:动画效果 anim cond focus 要求 animation targe mat 时间 WPF基础之样式 标签:动画效果 anim cond focus 要求 animation targe mat 时间 原文地址:https://www.cnblogs.com/lovezhangyu/p/10229250.html 样式基础
Window.Resources>
FontFamily x:Key="ButtonFontFamily">Times New RomanFontFamily>
sys:Double x:Key="ButtonFontSize">18sys:Double>
FontWeight x:Key="ButtonFontWeight">BoldFontWeight>
Window.Resources>
Grid Name="gird1" ShowGridLines="True">
Button FontFamily="{StaticResource ButtonFontFamily}" FontSize="{StaticResource ButtonFontSize}" FontWeight="{StaticResource ButtonFontWeight}">
Button>
Grid>
Window.Resources>
Style x:Key="BigFontButtonStyle">
Setter Property="Control.FontFamily" Value="Times New Roman"/>
Setter Property="Control.FontSize" Value="18"/>
Setter Property="Control.FontWeight" Value="Bold"/>
Style>
Window.Resources>
Grid Name="gird1" ShowGridLines="True">
Button Style="{StaticResource BigFontButtonStyle}">
Button>
Grid>
设置属性
Setter Property="Control.Background">
Setter.Value>
ImageBrush TileMode="Tile" ViewboxUnits="Absolute" Viewport="0 0 32 32" ImageSource="" Opacity="0.3">ImageBrush>
Setter.Value>
Setter>
创建只应用按钮的样式
Style x:Key="BigFontButtonStyle" TargetType="Button">
Setter Property="FontFamily" Value="Times New Roman"/>
Setter Property="FontSize" Value="18"/>
Setter Property="FontWeight" Value="Bold"/>
Style>
关联事件处理程序
Window.Resources>
Style x:Key="MouseOverHighlightStyle">
EventSetter Event="TextBlock.MouseEnter" Handler="element_MouseEnter">EventSetter>
EventSetter Event="TextBlock.MouseLeave" Handler="element_MouseLeave">EventSetter>
Style>
Window.Resources>
Grid Name="gird1" ShowGridLines="True">
TextBlock Style="{StaticResource MouseOverHighlightStyle}">TextBlock>
Grid>
多层样式
Window.Resources>
Style x:Key="BigFontButtonStyle">
Setter Property="Control.FontFamily" Value="Times New Roman"/>
Setter Property="Control.FontSize" Value="18"/>
Setter Property="Control.FontWeight" Value="Bold"/>
Style>
Style x:Key="NewBigButtonBigFontStyle" BasedOn="{StaticResource BigFontButtonStyle}">
Setter Property="Control.Foreground" Value="Red"/>
Setter Property="Control.Background" Value="DarkBlue"/>
Style>
Window.Resources>
Grid Name="gird1" ShowGridLines="True">
TextBlock Style="{StaticResource NewBigButtonBigFontStyle}">testTextBlock>
Grid>
通过类型自动应用样式
Window.Resources>
Style TargetType="Button">
Setter Property="Control.FontFamily" Value="Times New Roman"/>
Setter Property="Control.FontSize" Value="18"/>
Setter Property="Control.FontWeight" Value="Bold"/>
Style>
Window.Resources>
Grid Name="gird1" ShowGridLines="True">
Button>oneButton>
Button Style="{x:Null}">twoButton>
Button>threeButton>
Grid>
触发器
名称
说明
Trigger
这是一种最简单的触发器。可以监测依赖项属性的变化,然后设置器改变样式。
MultiTrigger
与Trigger类似,但这种触发器联合了多个条件。只有满足了所有这些条件,才会启动触发器。
DataTrigger
这种触发器使用数据绑定,与Trigger类似,只不过监视的是任意绑定数据的变化。
MultiDataTrigger
联合多个数据触发器。
EventTrigger
这是最复杂的触发器。当事件发生时,这种触发器应用动画。
简单触发器
Window.Resources>
Style x:Key="BigFontButton">
Style.Setters>
Setter Property="Control.FontFamily" Value="Times New Roman"/>
Setter Property="Control.FontSize" Value="18"/>
Setter Property="Control.FontWeight" Value="Bold"/>
Style.Setters>
Style.Triggers>
Trigger Property="Control.IsFocused" Value="True">
Setter Property="Control.Foreground" Value="DarkRed"/>
Trigger>
Style.Triggers>
Style>
Window.Resources>
Style.Triggers>
MultiDataTrigger>
MultiDataTrigger.Conditions>
Condition Property="Control.IsFocused" Value="true"/>
Condition Property="Control.IsMouseOver" Value="true"/>
MultiDataTrigger.Conditions>
MultiDataTrigger.Setters>
Setter Property="Control.Foreground" Value="DarkRed"/>
MultiDataTrigger.Setters>
MultiDataTrigger>
Style.Triggers>
事件触发器
Window.Resources>
Style x:Key="BigFontButton">
Style.Setters>
Setter Property="Control.FontFamily" Value="Times New Roman"/>
Setter Property="Control.FontSize" Value="18"/>
Setter Property="Control.FontWeight" Value="Bold"/>
Style.Setters>
Style.Triggers>
EventTrigger RoutedEvent="Mouse.MouseEnter">
EventTrigger.Actions>
BeginStoryboard>
Storyboard>
DoubleAnimation Duration="0:0:3" Storyboard.TargetProperty="FontSize"
To="50"/>
Storyboard>
BeginStoryboard>
EventTrigger.Actions>
EventTrigger>
EventTrigger RoutedEvent="Mouse.MouseLeave">
EventTrigger.Actions>
BeginStoryboard>
Storyboard>
DoubleAnimation Duration="0:0:3" Storyboard.TargetProperty="FontSize"/>
Storyboard>
BeginStoryboard>
EventTrigger.Actions>
EventTrigger>
Style.Triggers>
Style>
Window.Resources>
Grid Name="gird1" ShowGridLines="True">
Button HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource BigFontButton}">onesfsfsfsfsdfsdfButton>
Grid>