C# MouseEventArgs.GetPosition(…)未返回WPF中的相对位置
我在一个应用程序中有两个事件处理程序,它们提供“拖动”类型的操作,而不是实际的拖动事件。为此,我有事件处理程序来处理MouseDown和MouseMove事件C# MouseEventArgs.GetPosition(…)未返回WPF中的相对位置,c#,wpf,winforms,wpf-controls,C#,Wpf,Winforms,Wpf Controls,我在一个应用程序中有两个事件处理程序,它们提供“拖动”类型的操作,而不是实际的拖动事件。为此,我有事件处理程序来处理MouseDown和MouseMove事件 我遇到的问题是,我需要在MouseMove事件期间利用鼠标的相对位置。但是,当我调用GetPosition时,它返回0,0。。。在MouseEventArgs上,但在MouseButtonEventArgs上调用时工作得非常好。为什么鼠标事件的问题在于它们并非只发生在你所看到的上下文中,同样的事件到处都会发生。如果其他元素设置MouseE
我遇到的问题是,我需要在MouseMove事件期间利用鼠标的相对位置。但是,当我调用GetPosition时,它返回0,0。。。在MouseEventArgs上,但在MouseButtonEventArgs上调用时工作得非常好。为什么鼠标事件的问题在于它们并非只发生在你所看到的上下文中,同样的事件到处都会发生。如果其他元素设置MouseEventArgs.Handled=true。。。奇怪的事情开始发生了。我也遇到了同样的问题,e.GetPosition返回一个常量{-25;-170},但只有当窗口处于正常模式时,当maximized一切正常工作时。罪魁祸首是窗口级事件处理程序设置e.Handled=true;
因此,请检查应用程序在其他地方使用鼠标事件一些XAML?检查画布和UserControl的关系。UserControl有一个StackPanel,它将画布作为子级。它是按程序构建的我知道这很糟糕:。
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
}