September 05, 2006

WPF - Triggers

Triggers

  • a declarative mechanism that specifies how a control should respond to certain stimuli.

  • Trigger can be declared inside a style or a template

  • The declarative nature of triggers enables designers to create a user interface that responds to actions without the need for code from a developer

Example:

Following example shows hos to use trigger to have animation effect with the Eclipse.

<Page x:Class="TestXBAP.Page1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Page1" Height="108.8" Width="329.6">

<Ellipse Fill="Navy" Height="100">

<Ellipse.Triggers>

<EventTrigger

RoutedEvent="Ellipse.Loaded">

<BeginStoryboard>

<Storyboard TargetProperty=

"(Ellipse.Width)">

<DoubleAnimation From="20" To="200"

Duration="0:0:5"

RepeatBehavior="Forever" />

</Storyboard>

</BeginStoryboard>

</EventTrigger>

</Ellipse.Triggers>

</Ellipse>

</Page>