2012-09-28 120 views
3

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

当前的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

改为在MouseOver事件上使用EventTrigger,并使用BooleanAnimationUsingKeyFrames代替Storyboard。在Storyboard的时间轴中,您可以使用KeyFrame,以便动画在影响要更改的属性之前等待一段时间。

+0

感谢您的想法。我发布了我确定的代码,但是当你第一次,这个想法有很好的腿你就可以得到答案! :) –

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)