开发者

UserControl as ListBoxItem and IsSelected

开发者 https://www.devze.com 2022-12-28 17:31 出处:网络
I have a usercontrol that I want to use as a ListBoxItem. <ListBox.ItemTemplate> <DataTemplate>

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>
              开发者_如何学Python           <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.


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 :)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号