c# - 如何使用 C#/WPF 为 MouseEnter 和 MouseLeave 事件上的 ListBox 项目设置动画?

标签 c# wpf animation listboxitem

我无法通过列表项的 C# 代码捕获/触发 OnMouseEnter 或 OnMouseLeave 事件。明确地说,我不需要 OnSelectedItem 事件。

我想要做的是能够处理 ListBoxItem 的 OnMouseEnter 和 OnMouseLeave 事件,这将为该 ListBoxItem 启动 DoubleAnimation - 我想在 MouseEnter 上放大其字体并在 MouseLeave 上恢复到原始大小。

有什么想法吗?谢谢。

最佳答案

像这样的东西(作为 ListBox 的 DataTemplate 的一部分):

<DataTemplate.Triggers>
    <EventTrigger
        SourceName="BorderControl"
        RoutedEvent="TextBlock.MouseEnter">
        <BeginStoryboard>
            <Storyboard>
                <ColorAnimation Storyboard.TargetName="BorderControl"
                    Storyboard.TargetProperty="Background.Color"
                    To="DarkRed" Duration="00:00:00.2" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
    <EventTrigger
        SourceName="BorderControl"
        RoutedEvent="TextBlock.MouseLeave">
        <BeginStoryboard>
            <Storyboard>
                <ColorAnimation Storyboard.TargetName="BorderControl"
                    Storyboard.TargetProperty="Background.Color"
                    To="WhiteSmoke" Duration="00:00:00.2" />
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</DataTemplate.Triggers>

通过http://www.dotnet-blog.com/index.php/2009/01/29/how-to-style-and-animate-a-wpf-listbox/

关于c# - 如何使用 C#/WPF 为 MouseEnter 和 MouseLeave 事件上的 ListBox 项目设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/942149/

相关文章:

c# - 在另一个等待中使用等待

wpf - 有没有比网格更好的方法来排列 WPF 中的控件?

wpf - WPF 动画上的像素化​​和破损文本

iOS/Swift : Use UIView. animate 动画文本从 UITextView 添加/删除

css - 在无限 CSS3 动画期间暂停

c# - 回发后如何检查表单

c# - System.Security.Claims 命名空间的成员不可用?

c# - 以编程方式为桌面 "shortcut"创建快捷键组合

wpf - 使用 Key.App 打开时上下文菜单消失

c# - 将项目列表绑定(bind)到 Datagrid 单元格