views:

301

answers:

2

I'm building a custom control in Silverlight and I want one of the fields to animate to the value of a DependencyProperty when that property is changed. More specifically, I have particular item in my Control Template that I want to animate to the color of the Background whenever the background changes color. So, what I have is:

<ControlTemplate TargetType="local:MyType">
                <Grid x:Name="PART_RootElement">
                    <Grid.Resources>
                        <Storyboard x:Name="PART_FillAnimation">
                            <ColorAnimationUsingKeyFrames
                                 BeginTime="00:00:00"
                                 Storyboard.TargetName="PART_MainPath"
                           Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                <EasingColorKeyFrame
                                    x:Name="PATH_FillKeyframe"
                                    KeyTime="00:00:01" 
                                    Value="{TemplateBinding Background}"/>
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </Grid.Resources>
                    <!-- the rest of the template -->

I'm triggering the animation in the custom control code, but when the animation starts, it doesn't look like the Value is updating. I was just wondering if I'm missing something or if it is at all possible to apply TemplateBinding to resources in my ControlTemplate.

(I'm currently using a work-around of manually assigning the Background to the EasingColorKeyFrame Value, but the TemplateBinding solution would be so much cleaner.)

A: 

Hi MatthiasS, did you find any solutions to this problem? I'm in the same boat now and wondering if I can apply templatebinding to story board values.

beaudetious
A: 

Have a look at Expression Blend Samples as a possible solution to your problem. There are a number of Interactivity classes that you could use within your ControlTemplate to create the effect your looking for. The documentation is not great, but descriptions in the Object Browser should give you some more clues :)

For example, I have a ListBox ItemTemplate that contains a ControlStoryboardAction Behaviour. The trigger for this Behaviour is a DataTrigger which fires when a DataContext field contains a specific value. (In my case when Severity=="High") The trigger then Plays a Storyboard within the ItemTemplate.

<i:Interaction.Triggers>                                
<is:DataTrigger Binding="{Binding Severity, Mode=OneWay}" Value="High">
    <im:ControlStoryboardAction Storyboard="{StaticResource flashLight}" IsEnabled="True" />
</is:DataTrigger>

The following namespaces are referenced:

  1. <i: - System.Windows.Interactivity
  2. <is: - Expression.Samples.Interactivity (available from the link above. I am using the July 2009 release for SL3)
  3. <im: - Microsoft.Expression.Interactivity.Media
IsolatedStorage