WPF Behavior
前提条件
- 下载Wpf Behavior库 - Microsoft.Xaml.Behaviors.Wpf
tip: b 是我对应Behavior的代号xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
介绍
<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded">
<b:CallMethodAction MethodName="Method" TargetObject="{Binding}" />
</b:EventTrigger>
</b:Interaction.Triggers>
这是一个常见的用法,Iteraction类中定义了两个依赖属性
- Triggers
- Behaviors
源码
private static readonly DependencyProperty TriggersProperty = DependencyProperty.RegisterAttached("ShadowTriggers",typeof(TriggerCollection),typeof(Interaction),new FrameworkPropertyMetadata(new PropertyChangedCallback(OnTriggersChanged)));
private static readonly DependencyProperty BehaviorsProperty = DependencyProperty.RegisterAttached("ShadowBehaviors", typeof(BehaviorCollection),typeof(Interaction),new FrameworkPropertyMetadata(new PropertyChangedCallback(OnBehaviorsChanged)));
Trigger
Trigger的类型为TriggerCollection,可以为其添加多个TriggerBase,Behavior包为我们提供了多种Trigger,一下介绍一些常用的Trigger
EventTrigger
事件触发,EventName
为事件名称,SourceObject
为触发事件的对象,默认为附加的依赖属性
<b:EventTrigger EventName="Click" SourceObject="{Binding ElementName=button}"/>
DataTrigger
数据触发,Binding
为绑定数据,Value
为绑定数据中的值,当Binding
的值等于Value
时触发
<b:DataTrigger Binding="{Binding ElementName=checkBox, Path=IsChecked}" Value="true" />
PropertyChangedTrigger
属性改变触发,Binding
为绑定数据,当Binding
的值改变时触发
<b:PropertyChangedTrigger Binding="{Binding ElementName=checkBox, Path=IsChecked}"/>
Action
CallMethodAction
调用方法,MethodName
为方法名称,TargetObject
为方法所属的对象,默认为附加的依赖属性
<b:CallMethodAction MethodName="Method" TargetObject="{Binding Object}" />
InvokeCommandAction
<b:InvokeCommandAction Command="{Binding TestActiveCommand}" />
ControlStoryboardAction
ControlStoryboardAction
表示将在执行时更改指定Storyboard
状态的操作。使用此行为可以使用ControlStoryboard
属性触发和控制指定的Storyboard
ControlStoryboardOption 枚举
方法名称 | 描述 |
---|---|
Start | 开始播放动画 |
Stop | 停止播放动画 |
Pause | 暂停播放动画 |
Resume | 恢复播放动画 |
SkipToFill | 跳过动画 |
TogglePlayPause | 切换播放和暂停 |
<UserControl.Resources>
<Storyboard x:Key="StoryboardSample" AutoReverse="True" RepeatBehavior="Forever">
<DoubleAnimation Duration="0:0:2.2" To="0.35"
Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)"
Storyboard.TargetName="StoryboardRectangle" d:IsOptimized="True"/>
<DoubleAnimation Duration="0:0:2.2" To="0.35"
Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)"
Storyboard.TargetName="StoryboardRectangle" d:IsOptimized="True"/>
</Storyboard>
</UserControl.Resources>
<Rectangle x:Name="StoryboardRectangle" />
<Button x:Name="button">
<Behaviors:Interaction.Triggers>
<Behaviors:EventTrigger EventName="Click">
<Behaviors:ControlStoryboardAction Storyboard="{StaticResource StoryboardSample}" ControlStoryboardOption="TogglePlayPause"/>
</Behaviors:EventTrigger>
</Behaviors:Interaction.Triggers>
</Button>
GoToStateAction
GoToStateAction
表示在触发时会将FrameworkElement(即Button)
转换为指定的VisualState
的操作。此行为会将TargetObject
或TargetName
指定的元素更改为可视状态StateName
。除非UseTrantions
设置为false
,否则当前状态和新状态之间的任何转换都将播放。
<Button x:Name="sampleStateButton">
<Button.Resources>
<Style TargetType="Button" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="BaseGrid">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Normal">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="BaseGrid" Storyboard.TargetProperty="Opacity" To="1.0" From="1.0"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="BaseGrid" Storyboard.TargetProperty="Opacity" To="0.25" From="1.0"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Resources>
</Button>
<CheckBox x:Name="checkBox">
<Behaviors:Interaction.Triggers>
<Behaviors:DataTrigger Binding="{Binding IsChecked, ElementName=checkBox}" Value="False">
<Behaviors:GoToStateAction StateName="Normal" TargetObject="{Binding ElementName=sampleStateButton}"/>
</Behaviors:DataTrigger>
<Behaviors:DataTrigger Binding="{Binding IsChecked, ElementName=checkBox}" Value="True">
<Behaviors:GoToStateAction StateName="Disabled" TargetObject="{Binding ElementName=sampleStateButton}"/>
</Behaviors:DataTrigger>
</Behaviors:Interaction.Triggers>
</CheckBox>
Behavior
Behavior是一个行为,当事件发生时,会触发Behavior中的行为
ConditionBehavior
条件行为,ConditionalExpression
为条件表达式,当条件表达式为真时,触发Behavior中的行为
<b:Interaction.Triggers>
<b:EventTrigger EventName="Click" SourceObject="{Binding ElementName=Button}">
<b:Interaction.Behaviors>
<b:ConditionBehavior>
<b:ConditionalExpression>
<b:ComparisonCondition LeftOperand="{Binding Path=Items.Count, ElementName=TabControl}" RightOperand="0" />
</b:ConditionalExpression>
</b:ConditionBehavior>
</b:Interaction.Behaviors>
<b:ChangePropertyAction/>
</b:EventTrigger>
</b:Interaction.Triggers>
DataStateBehavior
当行为绑定到的数据等于某个值时,DataStateBeact
执行操作。每当更新关联的绑定时,此行为都会检查绑定结果是否等于某个值。如果两个值相等,则转换为TrueState
。否则,它会转换为FalseState
。
<Button x:Name="sampleStateButton">
<Button.Resources>
<Style TargetType="Button" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid x:Name="BaseGrid" Background="DeepPink">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Normal">
<Storyboard>
<ColorAnimation Storyboard.TargetName="BaseGrid" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="DarkBlue" From="DeepPink" />
</Storyboard>
</VisualState>
<VisualState x:Name="Blue">
<Storyboard>
<ColorAnimation Storyboard.TargetName="BaseGrid" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" To="DeepPink" From="DarkBlue" Duration="Forever" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Behaviors:Interaction.Behaviors>
<Behaviors:DataStateBehavior Binding="{Binding Text, ElementName=MyText}" Value="" TrueState="Normal" FalseState="Blue" />
</Behaviors:Interaction.Behaviors>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Resources>
</Button>
标签:触发,动画,绑定,介绍,Trigger,Behavior,播放
From: https://www.cnblogs.com/yinyuessh/p/18181672