触发时间延迟

时间:2012-09-28 10:24:20

标签: wpf vb.net xaml mouseover expander

我希望在我的表单上的WPF扩展器上附加一个鼠标悬停事件的时间延迟(后面是VB.NET代码支持的xaml)。这个鼠标悬停事件本质上会触发扩展而不是点击 - 但我想在内容扩展之前等待一段时间。到目前为止,我还没有设法通过更广泛的互联网找到解决这个问题的方法。

启用触发器的当前xaml代码是:

    <Style x:Key="HoverExpander" TargetType="{x:Type Expander}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="IsExpanded" Value="True" />
            </Trigger>
        </Style.Triggers>
    </Style>

然后将此样式应用于:

<Expander Style="{StaticResource HoverExpander}" 
          HorizontalAlignment="Right"
          ExpandDirection="Left" 
          Height="Auto" 
          Width="Auto">
           <!-- Content here -->
</Expander>

请注意,我已经剥离了其他美学(例如边框,gridrefs等以提高可读性)。

我认为应该有一些方法可以在MouseOver触发器上设置延迟但是没有太多运气找到它。这可以在xaml中设置,也可以作为后面代码中的事件。

我目前正在研究这个问题,所以当我找到解决方案时,我会在这里发布。同时感谢任何想法。谢谢!

2 个答案:

答案 0 :(得分:2)

使用MouseOver事件上的EventTrigger和带有BooleanAnimationUsingKeyFrames的Storyboard。在故事板的时间轴中,您可以拥有KeyFrame,以便动画在影响您想要更改的属性之前等待一段时间。

答案 1 :(得分:2)

这是我根据已经给出的想法确定的代码:

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

然后像以前一样申请。这已经过测试,可以在.NET 4.0中运行。如果您愿意,可以应用其他巧妙的技巧,我发现以下内容对于获取想法非常有帮助:

Animation Overview (MSDN)

Storyboards Overview (MSDN)