views:

118

answers:

2

I have a usercontrol that I want to use as a ListBoxItem.

<ListBox.ItemTemplate>
     <DataTemplate>
          <local:MyUserControl/>
     </DataTemplate>
</ListBox.ItemTemplate>

I'd like to play a storyboard when the usercontrol is unselected.

<UserControl.Resources>
     <Style TargetType="{x:Type UserControl}">
          <Style.Triggers>
               <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="False">
                    <DataTrigger.EnterActions>
                         <BeginStoryboard Storyboard="{StaticResource OnMouseLeaveSB}"/>
                    </DataTrigger.EnterActions>
               </DataTrigger>
          </Style.Triggers>
     </Style>
 </UserControl.Resources>

But the storyboard is never firing. Is there a better way to do this?

Edited to Add:

What I am really trying to accomplish this this:

When the mouse is over the UserControl, I want to play a storyboard (OnMouseEnterSB). When the mouse leaves the UserControl, I want to play another storyboard (OnMouseLeaveSB). I have all this working fine.

When the UserControl is selected, however, and the mouse leaves, I do NOT want to play the storyboard.

Finally, when the UserControl is unselected, I want to play the OnMouseLeaveSB storyboard.

+1  A: 

I don't have WPF experience rather I am a Silverlgiht girl and in Silverlight the thing you are describing is called "VisualStateManager" (just binged it, it is also available in WPF).

With VSM you would define different visual appearances for each "state" of your (user)control (mouseover, mouseleft, normal) and also depending on the previous and/or next state you can define different transitions between those states (or you may use a default transition for moving between all different states).

Read this blog-post by Tim Heuer. Describes it well with many screenshots :). You may also want to check out this link.

Using VSM the states and animations are a part of the control not the application with bunch of event-handlers and animation.Begin() calls. I really like and recommend it :)

texmex5
Thanks. The VSM is exactly what I needed. I had come across it a couple times, but never payed attention to what it really was.
mattjf
+1  A: 

If i understand your question correctly, you want to play this animation 'OnMouseLeaveSB' when any ListViewItem loses selection. But in your trigger you're playing the animation for all the unselected items. Hence even if this works, it will not be the one you wanted.

Reason why the storyboard does not fire is that the default BlueHighlight hides your animation. A hack to get rid of this would be to set the border color which is explained here www.HereIsYourLink.com

To achieve what you want, you'll have to insert your storyboard in Trigger.ExitActions with the IsSelected value 'True'.

If you're not in a hurry, take a look at VSM too.

Veer
Thanks for the answer. I forgot to mention, I had actually taken care of the border color and everything. The problem was more the sequence of events, and controlling which one played at which time. The VSM is exactly what I need
mattjf