Upload
eyal-vardi
View
3.617
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Triggers, actions & behaviors in XAML
Citation preview
Triggers, Actions Triggers, Actions & Behaviors& Behaviors
Eyal VardiEyal VardiCEO E4D Solutions LTDCEO E4D Solutions LTDMicrosoft MVP Visual C#Microsoft MVP Visual C#blog: www.eVardi.comblog: www.eVardi.com
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
AgendaAgenda
ActionsActions
TriggersTriggers
BehaviorsBehaviors
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Old WPF Old WPF ( No Silverlight )( No Silverlight )
<Button Content="Click Me"> <Button.Triggers>
<EventTrigger RoutedEvent="Click"> <EventTrigger.Actions> <BeginStoryboard Storyboard="{StaticResource sbSpin}"/> </EventTrigger.Actions> </EventTrigger>
<MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True" /> <Condition Property="Content" Value="{x:Null}" /> </MultiTrigger.Conditions> <Setter Property="Background" Value="Yellow" /> </MultiTrigger> </Button.Triggers> </Button>
<Button Content="Click Me"> <Button.Triggers>
<EventTrigger RoutedEvent="Click"> <EventTrigger.Actions> <BeginStoryboard Storyboard="{StaticResource sbSpin}"/> </EventTrigger.Actions> </EventTrigger>
<MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="True" /> <Condition Property="Content" Value="{x:Null}" /> </MultiTrigger.Conditions> <Setter Property="Background" Value="Yellow" /> </MultiTrigger> </Button.Triggers> </Button>
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
.NET 3.5 WPF .NET 3.5 WPF ( When __, Do __ )( When __, Do __ )
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Action WayAction Way<Button Content="Click Me"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <E4D:MyAction TargetName="lbTrace" /> </i:EventTrigger></i:Interaction.Triggers>
</Button>
<Button Content="Click Me"> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <E4D:MyAction TargetName="lbTrace" /> </i:EventTrigger></i:Interaction.Triggers>
</Button>
public class MyAction : TargetedTriggerAction<DependencyObject>{ public MyAction() { ... }
protected override void Invoke(object o) { ... }}
public class MyAction : TargetedTriggerAction<DependencyObject>{ public MyAction() { ... }
protected override void Invoke(object o) { ... }}
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Perform on Perform on somebody elsesomebody else
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
ActionAction
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Trigger Trigger ( When )( When )
Invokes a bunch of actions at a point when it Invokes a bunch of actions at a point when it decides to do that.decides to do that. Event TriggerEvent Trigger Timer TriggerTimer Trigger Size TriggerSize Trigger
The Attach & Detach connect toThe Attach & Detach connect tothe target.the target.
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Timer Trigger SampleTimer Trigger Samplepublic class TimerTrigger : TriggerBase<DependencyObject>{ public TimeSpan Interval { get; set; }
protected override void OnAttached() { timer = new DispatcherTimer(); timer.Interval = Interval; timer.Tick += OnTick; timer.Start(); }
void OnTick(object sender, EventArgs e) { this.InvokeActions(null); }
protected override void OnDetaching() { timer.Tick -= OnTick; timer = null; } DispatcherTimer timer;}
public class TimerTrigger : TriggerBase<DependencyObject>{ public TimeSpan Interval { get; set; }
protected override void OnAttached() { timer = new DispatcherTimer(); timer.Interval = Interval; timer.Tick += OnTick; timer.Start(); }
void OnTick(object sender, EventArgs e) { this.InvokeActions(null); }
protected override void OnDetaching() { timer.Tick -= OnTick; timer = null; } DispatcherTimer timer;}
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Custom TriggerCustom Trigger
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
BehaviorBehavior
Behavior Behavior don’t need don’t need the “the “WhenWhen” ( Trigger ),” ( Trigger ),and and don’t have don’t have the the InvokeInvoke..
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Behavior with CommandBehavior with Command
public class MethodBehavior : Behavior<DependencyObject>{ public ICommand Command { get; set; }
public MethodBehavior() {
Command = new ActionCommand( MyMethod ); }
private void MyMethod() { // TODO: }}
public class MethodBehavior : Behavior<DependencyObject>{ public ICommand Command { get; set; }
public MethodBehavior() {
Command = new ActionCommand( MyMethod ); }
private void MyMethod() { // TODO: }}
<Button Content="Click Me“> <i:Interaction.Behaviors> <E4D:MethodBehavior>
<i:Interaction.Triggers><i:KeyTrigger SourceName="..."> <i:InvokeCommandAction
CommandName="Command" /></i:KeyTrigger>
</i:Interaction.Triggers></E4D:MethodBehavior>
</i:Interaction.Behaviors></Button>
<Button Content="Click Me“> <i:Interaction.Behaviors> <E4D:MethodBehavior>
<i:Interaction.Triggers><i:KeyTrigger SourceName="..."> <i:InvokeCommandAction
CommandName="Command" /></i:KeyTrigger>
</i:Interaction.Triggers></E4D:MethodBehavior>
</i:Interaction.Behaviors></Button>
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
BehaviorsBehaviors
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Blend BehaviorsBlend Behaviors
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Fluid Move BehaviorFluid Move Behavior
Behavior that watches an element (or a set of Behavior that watches an element (or a set of elements) for layout changes, and moves the elements) for layout changes, and moves the element smoothly to the new position when element smoothly to the new position when needed.needed.
<ei:FluidMoveBehavior Duration="00:00:01" AppliesTo="Children"> <ei:FluidMoveBehavior.EaseY> <BackEase EasingMode="EaseOut" Amplitude="0.5"/> </ei:FluidMoveBehavior.EaseY> <ei:FluidMoveBehavior.EaseX> <BackEase EasingMode="EaseOut" Amplitude="0.5"/> </ei:FluidMoveBehavior.EaseX></ei:FluidMoveBehavior>
<ei:FluidMoveBehavior Duration="00:00:01" AppliesTo="Children"> <ei:FluidMoveBehavior.EaseY> <BackEase EasingMode="EaseOut" Amplitude="0.5"/> </ei:FluidMoveBehavior.EaseY> <ei:FluidMoveBehavior.EaseX> <BackEase EasingMode="EaseOut" Amplitude="0.5"/> </ei:FluidMoveBehavior.EaseX></ei:FluidMoveBehavior>
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Fluid Move BehaviorFluid Move Behavior
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Fluid Move BehaviorFluid Move Behavior
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Fluid Layout in VSMFluid Layout in VSM
Animation between states.Animation between states. - Transition Effect- Transition Effect - Easing Function - Easing Function
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Fluid Layout in VSMFluid Layout in VSM
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Layout States & TransitionLayout States & Transition
Layout States animate the state changes for Layout States animate the state changes for you at the appropriate times, as your items are you at the appropriate times, as your items are added to or removed from the list. added to or removed from the list.
Edit the ItemContainerStyle and ItemsPanel.Edit the ItemContainerStyle and ItemsPanel.
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Layout States & Layout States & TransitionTransition
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
FluidMoveTagSetBehavior FluidMoveTagSetBehavior
© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
Fluid Move Tag Set Fluid Move Tag Set BehaviorBehavior