【问题标题】:MouseEventArgs.GetPosition(...) Not Returning Relative Position in WPFMouseEventArgs.GetPosition(...) 不返回 WPF 中的相对位置
【发布时间】:2014-11-06 04:00:43
【问题描述】:

我在应用程序中有两个事件处理程序,它们提供“拖动”类型的操作(不是实际的拖动事件)。为此,我有事件处理程序来处理 MouseDownMouseMove 事件。

    private void OnMouseDown(object sender,MouseButtonEventArgs e)
    {
        //The following CORRECTLY gives the relative position
        //Note the MouseButtonEventArgs type
        Point position = e.GetPosition(this.myCanvas);
        Point relativePosition = e.GetPosition(this.myUsercontrol);

        //Do something with position and relative position
    }

    private void OnMouseMove(object sender,MouseEventArgs e)
    {
        //The following INCORRECTLY gives the relative position.
        //Note the MouseEventArgs type

        Point position = e.GetPosition(this.myCanvas);
        Point relativePosition = e.GetPosition(this.myUsercontrol);

        //Do something with position and relative position
    }

我遇到的问题是我需要在 MouseMove 事件期间利用鼠标的相对位置。但是,当我在 MouseEventArgs 上调用 GetPosition(...) 时,它返回 (0,0),但在 MouseButtonEventArgs 上调用时工作得非常好。为什么会

【问题讨论】:

  • 一些 XAML?检查 Canvas 和 UserControl 的关系。
  • UserControl 有一个 StackPanel,它以 Canvas 作为子项。它是以编程方式构建的(我知道这很糟糕:))。

标签: c# wpf winforms wpf-controls


【解决方案1】:

鼠标事件的问题在于它们不仅仅发生在您正在查看的上下文中,同样的事件在任何地方都会发生。如果其他一些元素设置 MouseEventArgs.Handled = true ......那么奇怪的事情就会开始发生。我遇到了同样的问题,e.GetPosition() 返回一个常量 {-25;-170},但只有当窗口处于正常模式时,最大化时一切正常。罪魁祸首是窗口级事件处理程序设置 e.Handled = true; 因此,请检查您的应用程序在哪里使用鼠标事件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    相关资源
    最近更新 更多