你好,
如何在 WPF 窗口内移动(拖动)网格面板?网格面板没有位置或位置或 X 和 Y 坐标属性。我所看到的只是使用鼠标将网格面板从其当前位置移动到新位置,以便显示在其下方“隐藏”的控件。
任何指针?
非常感谢。
以下是一些帮助您入门的代码示例:
在 XAML 中:
创建一个网格并在其上定义一个渲染变换:
<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>
</Grid>
命名您希望网格在其中移动的控件:
<Window x:Name="window" ...>
<Grid x:Name="grid"...
</Window>
在后面的代码中:
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();
}
基于 Josh G 的回答
如果您想移动一个网格,乔希的回答非常好,但缺乏移动多个元素的能力。
这是分别移动几个元素的方法
XAML
<Grid x:Name="gridHost">
<Grid x:Name="gridBlue" Background="Blue" Width="100" Height="100" MouseDown="Grid_MouseDown" MouseMove="Grid_MouseMove" MouseUp="Grid_MouseUp" Margin="-100,0,0,0">
<Grid.RenderTransform>
<TranslateTransform/>
</Grid.RenderTransform>
</Grid>
<Grid x:Name="gridRed" Background="Red" Width="100" Height="100" MouseDown="Grid_MouseDown" MouseMove="Grid_MouseMove" MouseUp="Grid_MouseUp" Margin="100,0,0,0">
<Grid.RenderTransform>
<TranslateTransform/>
</Grid.RenderTransform>
</Grid>
</Grid>
背后的代码
Point m_start;
Vector m_startOffset;
private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as Grid;
TranslateTransform translate = element.RenderTransform as TranslateTransform;
m_start = e.GetPosition(gridHost);
m_startOffset = new Vector(translate.X, translate.Y);
element.CaptureMouse();
}
private void Grid_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = sender as Grid;
TranslateTransform translate = element.RenderTransform as TranslateTransform;
if (element.IsMouseCaptured)
{
Vector offset = Point.Subtract(e.GetPosition(gridHost), m_start);
translate.X = m_startOffset.X + offset.X;
translate.Y = m_startOffset.Y + offset.Y;
}
}
private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as Grid;
element.ReleaseMouseCapture();
}
只需将网格面板放在画布内而不是直接放在窗口中 - 这将为其提供 X/Y 坐标。
然后,您可以使用自定义附加属性实现拖放行为,例如此处详述(http://www.deepcode.co.uk/archive/2008/11/16/using-attached-properties-to-compose-new -behaviour.aspx )
public void dragme(object sender, MouseButtonEventArgs e)
{
if (_Move.IsChecked == true)
db.Attach((DependencyObject)sender);
}
//// MouseDragElementBehavior db;
private void canvass_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (_Move.IsChecked == true && filmgrid.Visibility == Visibility.Visible)// == true)
{
filmgrid.PreviewMouseDown += new MouseButtonEventHandler(dragme);
}
}