UserControl 作为 ListBoxItem 和 IsSelected

发布于 2024-08-31 18:02:10 字数 1140 浏览 4 评论 0 原文

我有一个用户控件,我想将其用作 ListBoxItem。

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

我想在取消选择用户控件时播放故事板。

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

但故事板永远不会触发。有更好的方法吗?

编辑添加:

我真正想要实现的目标是:

当鼠标悬停在 UserControl 上时,我想播放情节提要(OnMouseEnterSB)。当鼠标离开 UserControl 时,我想播放另一个故事板(OnMouseLeaveSB)。我这一切都工作得很好。

然而,当选择用户控件并且鼠标离开时,我不想播放故事板。

最后,当取消选择 UserControl 时,我想播放 OnMouseLeaveSB 故事板。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

居里长安 2024-09-07 18:02:10

我没有WPF经验,而是我是一个Silverlgiht女孩,在Silverlight中你所描述的东西被称为“VisualStateManager”(只是狂饮它,它也可以在WPF中使用)。

使用 VSM,您可以为(用户)控件的每个“状态”(鼠标悬停、鼠标左移、正常)定义不同的视觉外观,并且根据上一个和/或下一个状态,您可以定义这些状态之间的不同转换(或者您可以使用在所有不同状态之间移动的默认转换)。

阅读此内容 Tim Heuer 的博客文章。通过许多屏幕截图很好地描述了它:)。您可能还想查看此链接

使用VSM,状态和动画是控件的一部分,而不是具有一堆事件处理程序和animation.Begin()调用的应用程序的一部分。我真的很喜欢并推荐它:)

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

谈场末日恋爱 2024-09-07 18:02:10

如果我正确理解你的问题,你想在任何 ListViewItem 失去选择时播放此动画“OnMouseLeaveSB”。但在触发器中,您正在为所有未选定的项目播放动画。因此,即使这有效,它也不会是您想要的。

故事板不触发的原因是默认的 BlueHighlight 隐藏了您的动画。摆脱这个问题的一个技巧是设置边框颜色,解释如下www.HereIsYourLink.com

要实现您想要的目标,您必须在 Trigger.ExitActions 中插入故事板IsSelected 值为“True”。

如果您不着急,也可以看看 VSM。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文