【发布时间】:2014-11-06 04:00:43
【问题描述】:
我在应用程序中有两个事件处理程序,它们提供“拖动”类型的操作(不是实际的拖动事件)。为此,我有事件处理程序来处理 MouseDown 和 MouseMove 事件。
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