views:

264

answers:

2

I have a subclassed ListBox with a "SelectedItemChanging" dependency property that is set to a Storyboard. When the selected item is changed, I want to run this Storyboard on each item in the ListBox.

How is this possible with a single instance of Storyboard?

+1  A: 

Storyboards can be keyed and run from multiple triggers, and it works great as long as it's set up properly. If I am understanding you correctly, you're hoping to apply the storyboard to each individual ListBoxItem. In which case, why not make a style, and on that style's triggers, fire the storyboard.

Excuse my pseudocode.

<Storyboard x:Key="MyEnterStoryboard">
  <!-- Do Enter Work -->
</Storyboard>

<Storyboard x:Key="MyExitStoryboard">
  <!-- Do Exit Work -->
</Storyboard>

<Style TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <Trigger Property="SelectedItemChanging" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource MyEnterStoryboard}"/>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <BeginStoryboard Storyboard="{StaticResource MyExitStoryboard}"/>
            </Trigger.ExitActions>
        </Trigger>
    </Style.Triggers>
</Style>
Erode
Thanks for that, that makes sense. But how does this translate to Silverlight? As I understand it, SL does not support triggers.
Richard Szalay
Sorry Richard, saw the WPF tag and not the silverlight tag. I did some research and found that you might be able to use behaviors. Also, depending on what version of SL you're using, you might be able to use SOME triggers.
Erode
+1  A: 

WPF Storyboards have a Clone method. Silverlight doesn't have this but thought I'd post it just in case someone stumbles across this post looking for a WPF solution.

James Hay