我在网格中有一个 mainWindow,在这个网格中有几个 wpf UserControl。我还有三种方法可以通过鼠标控制网格内的移动。
MouseDown
,MouseMove
并且MouseUp
它工作正常。
当我WindowsFormsHost
在其中一个 UserControl 中添加一个时,它就不再起作用了。我可以移动窗口,但WindowsFormsHost
停留在同一个地方。
我想知道当我有WindowsFormsHost
内部时如何使用鼠标事件在网格内移动窗口。
提前致谢。
问候,费利佩。
PS。这些是我用来在网格内移动我的 UserControl 的方法。
public void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isDragging = true;
var draggableControl = sender as UserControl;
clickPosition = e.GetPosition(this);
draggableControl.CaptureMouse();
}
public void Control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isDragging = false;
var draggable = sender as UserControl;
draggable.ReleaseMouseCapture();
}
public void Control_MouseMove(object sender, MouseEventArgs e)
{
var draggableControl = sender as UserControl;
if (isDragging && draggableControl != null)
{
Point currentPosition = e.GetPosition(this.Parent as UIElement);
var transform = draggableControl.RenderTransform as TranslateTransform;
if (transform == null)
{
transform = new TranslateTransform();
draggableControl.RenderTransform = transform;
}
transform.X = currentPosition.X - clickPosition.X;
transform.Y = currentPosition.Y - clickPosition.Y;
}
}