我找到了允许在窗口中拖动网格的解决方案。但我正在使用MVVM。 如何在MVVM中完成?我需要使用什么?内容控制或其他方面?
<Grid x:Name="grid" Background="Blue"
Width="100" Height="100"
MouseDown="Grid_MouseDown" MouseMove="Grid_MouseMove" MouseUp="Grid_MouseUp">
<Grid.RenderTransform>
<TranslateTransform x:Name="tt"/>
</Grid.RenderTransform>
<Window x:Name="window" ...>
<Grid x:Name="grid"...
Point m_start;
Vector m_startOffset;
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
m_start = e.GetPosition(window);
m_startOffset = new Vector(tt.X, tt.Y);
grid.CaptureMouse();
}
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
if (grid.IsMouseCaptured)
{
Vector offset = Point.Subtract(e.GetPosition(window), m_start);
tt.X = m_startOffset.X + offset.X;
tt.Y = m_startOffset.Y + offset.Y;
}
}
private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
{
grid.ReleaseMouseCapture();
}
答案 0 :(得分:1)
MVVM与此无关。如果你想移动东西并从鼠标/窗口获取事件,这只能通过后面的代码完成 MVVM命令/ RelayCommand用于model / viewmodel / View处理。不适合鼠标,窗口互动。
你可以使用输入绑定等等,但基本上你正在做的是正确的方法