How to animate ListBox Items on MouseEnter and MouseLeave events using C#/WPF?

坚强是说给别人听的谎言 提交于 2019-11-30 18:07:52

问题


I can't capture/trigger OnMouseEnter or OnMouseLeave events through C# code for list items. To be clear, I don't need an OnSelectedItem event.

What I want to do is to be able to handle the OnMouseEnter and OnMouseLeave events for ListBoxItem which will start the DoubleAnimation for that ListBoxItem - I want to enlarge its font on MouseEnter and restore to original size on MouseLeave.

Any ideas? Thanks.


回答1:


Something like this (as part of the ListBox's 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>

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



来源:https://stackoverflow.com/questions/942149/how-to-animate-listbox-items-on-mouseenter-and-mouseleave-events-using-c-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!