Silverlight 3 introduced interaction triggers that, as far as I can tell, do what you want, but a little more complicated. However, there are very few examples.
If you do this manually, you need links to System.Windows.Interactivity and Microsoft.Expression.Interactions (from Blend 3, the class will be in the links tab if you installed it).
If you add triggers to Blend, it will automatically add them. This is called Behaviors in Silverlight 3, and you will find them in Blend under the Behaviors section of the Assets tab.
An example of how they work. Pay attention to the storyboard sitting in the resource of the second rectangle, I could not get it to work inside the ControlStoryboardAction.Storyboard, but it worked if I made a ContentControl rectangle and placed it in the template. It may be a mistake, or I'm missing something:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Class="SLTrigger.MainPage" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" xmlns:im="clr-namespace:Microsoft.Expression.Interactivity.Media;assembly=Microsoft.Expression.Interactions"> <Grid x:Name="LayoutRoot"> <StackPanel> <Rectangle Margin="5" Fill="Blue" Width="200" Height="100"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonDown"> <ic:ChangePropertyAction PropertyName="Fill" Duration="0"> <ic:ChangePropertyAction.Value> <SolidColorBrush Color="Red"/> </ic:ChangePropertyAction.Value> </ic:ChangePropertyAction> </i:EventTrigger> </i:Interaction.Triggers> </Rectangle> <Rectangle Margin="5" x:Name="AnimatedRectangle2" Fill="Blue" Width="200" Height="100"> <Rectangle.Resources> <Storyboard x:Key="AnimationStoryboard"> <ColorAnimation Storyboard.TargetName="AnimatedRectangle2" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" To="Red" AutoReverse="True" Duration="0:0:0.5" /> </Storyboard> </Rectangle.Resources> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonDown"> <im:ControlStoryboardAction ControlStoryboardOption="Play" Storyboard="{StaticResource AnimationStoryboard}"> </im:ControlStoryboardAction> </i:EventTrigger> </i:Interaction.Triggers> </Rectangle> <ContentControl> <ContentControl.Template> <ControlTemplate> <Rectangle Margin="5" x:Name="AnimatedRectangle3" Fill="Blue" Width="200" Height="100"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonDown"> <im:ControlStoryboardAction ControlStoryboardOption="Play"> <im:ControlStoryboardAction.Storyboard> <Storyboard> <ColorAnimation Storyboard.TargetName="AnimatedRectangle3" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" To="Red" AutoReverse="True" Duration="0:0:0.5" /> </Storyboard> </im:ControlStoryboardAction.Storyboard> </im:ControlStoryboardAction> </i:EventTrigger> </i:Interaction.Triggers> </Rectangle> </ControlTemplate> </ContentControl.Template> </ContentControl> <TextBlock TextAlignment="Center" Text="Click the rectangles" /> </StackPanel> </Grid> </UserControl>
There is nothing in the class file:
using System.Windows.Controls; namespace SLTrigger { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } } }
source share