如何在我的wpf用户控件上检测鼠标指针?

时间:2010-06-15 09:40:00

标签: wpf

我在后面的用户控制代码上使用了一些动画。

double height = canMain.ActualHeight - marqueeList.ActualHeight;
        marqueeList.Margin = new Thickness(0, height / 2, 0, 0);
        DoubleAnimation doubleAnimation = new DoubleAnimation();
        doubleAnimation.From = -marqueeList.ActualWidth;
        doubleAnimation.To = canMain.ActualWidth;
        doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
        doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(_marqueeTimeInSeconds));
        marqueeList.BeginAnimation(Canvas.RightProperty, doubleAnimation);

我希望当我将鼠标放在用户控件上时,动画应该停止。

3 个答案:

答案 0 :(得分:1)

您可以处理MouseMove事件并检查IsMouseOver属性

答案 1 :(得分:1)

如果已启动动画设置isControllable为true,则应该可以使用Storyboard.Stop()方法

Storyboard.Begin(this, true); 

如果您打算在某些情况下再次重新启动它,那么您还拥有Storyboard.Pause()方法。

看看这个:http://msdn.microsoft.com/en-us/library/ms742868.aspx

试试这个:

<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="False">
      <Trigger.EnterActions>
        <BeginStoryboard>
          <Storyboard>
            <!-- do your animation here (forever) -->
          </Storyboard>
        </BeginStoryboard>
      </Trigger.EnterActions>
      <Trigger.ExitActions>
        <BeginStoryboard>
          <Storyboard>
            <!-- fake animation with duration set to 0 -->
          </Storyboard>
        </BeginStoryboard>
      </Trigger.ExitActions>          
    </Trigger>               
  </Style.Triggers>   

答案 2 :(得分:0)

感谢您的回复!!

我找到了答案,我使用了鼠标输入和鼠标离开事件。

鼠标输入:

  Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Right)"));
            _storyBoard.Children.Add(doubleAnimation);
            _storyBoard.Pause(marqueeList);

鼠标离开:

    Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Right)"));
        _storyBoard.Children.Add(doubleAnimation);
        _storyBoard.Resume(marqueeList);