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

如何在wpf用户控件上检测鼠标指针?,wpf,Wpf,我在我的用户控制代码后面使用了一些动画 double height = canMain.ActualHeight - marqueeList.ActualHeight; marqueeList.Margin = new Thickness(0, height / 2, 0, 0); DoubleAnimation doubleAnimation = new DoubleAnimation(); doubleAnimation.From = -mar

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

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

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

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

如果您已开始将iscontrolable设置为true,则应该能够使用
故事板.stop()
方法

Storyboard.Begin(this, true); 
如果您计划在某些情况下重新启动脚本,那么还可以使用
脚本.Pause()
方法

看看这个:

也试试这个:

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

谢谢您的回复

我找到了我的答案,为此我使用了鼠标进入和鼠标离开事件

在鼠标上输入:

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

谢谢你的回复!!我更新了我的问题,现在我的代码也在那里,你能告诉我如何停止动画吗?我使用上面的代码开始动画,现在我想停止,你能根据上面的代码帮我吗