23
Triggers, Triggers, Actions & Actions & Behaviors Behaviors Eyal Vardi Eyal Vardi CEO E4D Solutions LTD CEO E4D Solutions LTD Microsoft MVP Visual C# Microsoft MVP Visual C# blog: www.eVardi.com blog: www.eVardi.com

Triggers, actions & behaviors in XAML

Embed Size (px)

DESCRIPTION

Triggers, actions & behaviors in XAML

Citation preview

Page 1: Triggers, actions & behaviors in XAML

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

Page 2: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

AgendaAgenda

ActionsActions

TriggersTriggers

BehaviorsBehaviors

Page 3: Triggers, actions & behaviors in XAML

© 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>

Page 4: Triggers, actions & behaviors in XAML

© 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 __ )

Page 5: Triggers, actions & behaviors in XAML

© 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) { ... }}

Page 6: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Perform on Perform on somebody elsesomebody else

Page 7: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

ActionAction

Page 8: Triggers, actions & behaviors in XAML

© 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.

Page 9: Triggers, actions & behaviors in XAML

© 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;}

Page 10: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Custom TriggerCustom Trigger

Page 11: Triggers, actions & behaviors in XAML

© 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..

Page 12: Triggers, actions & behaviors in XAML

© 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>

Page 13: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

BehaviorsBehaviors

Page 14: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Blend BehaviorsBlend Behaviors

Page 15: Triggers, actions & behaviors in XAML

© 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>

Page 16: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Fluid Move BehaviorFluid Move Behavior

Page 17: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Fluid Move BehaviorFluid Move Behavior

Page 18: Triggers, actions & behaviors in XAML

© 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

Page 19: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Fluid Layout in VSMFluid Layout in VSM

Page 20: Triggers, actions & behaviors in XAML

© 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.

Page 21: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Layout States & Layout States & TransitionTransition

Page 22: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

FluidMoveTagSetBehavior FluidMoveTagSetBehavior

Page 23: Triggers, actions & behaviors in XAML

© 2008 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Fluid Move Tag Set Fluid Move Tag Set BehaviorBehavior