Time Delay on Trigger

前端 未结 2 1624
感情败类
感情败类 2020-12-19 19:24

I wish to attach a time delay to a mouseover event on a WPF expander I have on my form (xaml supported by VB.NET code behind). This mouseover event essentially triggers the

相关标签:
2条回答
  • 2020-12-19 20:15

    Use an EventTrigger on the MouseOver event and a Storyboard with a BooleanAnimationUsingKeyFrames instead. In the Timeline of the Storyboard, you could have KeyFrames, so that the animation waits for some time before it affects the properties you want to change.

    0 讨论(0)
  • 2020-12-19 20:19

    This was the code I settled on - based on the ideas already given:

        <Style x:Key="HoverExpander" TargetType="{x:Type Expander}">            
            <Style.Setters>                
                <Setter Property="IsExpanded" Value="False"/><!-- Initially collapsed -->            
            </Style.Setters>
    
            <Style.Triggers>
                <!-- Impose a short delay (500ms) before expanding control -->
                <EventTrigger RoutedEvent="Expander.MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames
                                Storyboard.TargetProperty="IsExpanded"
                                Duration="0:0:0.5">
                                <DiscreteBooleanKeyFrame Value="True" KeyTime="100%"/><!-- I.E. after 500ms -->                             
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
                <!-- Collapse when mouse leaves control-->
                <EventTrigger RoutedEvent="Expander.MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <BooleanAnimationUsingKeyFrames
                                Storyboard.TargetProperty="IsExpanded"
                                Duration="0:0:0.1">
                                <DiscreteBooleanKeyFrame Value="False" KeyTime="0%"/><!-- I.E. Immediately -->
    
                            </BooleanAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    

    Then apply as before. This was tested and works in .NET 4.0. Other neat tricks could be applied if you do so wish, I found the following to be quite helpful in getting ideas:

    Animation Overview (MSDN)

    Storyboards Overview (MSDN)

    0 讨论(0)
提交回复
热议问题