你好,
我该如何在WPF窗口内移动(拖动)一个网格面板?网格面板没有位置或坐标属性。我只想使用鼠标将网格面板从当前位置移动到新位置,以便显示埋在其下面的控件。
有什么建议吗?
非常感谢。
以下是一些代码示例,可帮助您入门:
在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的回答
如果你想移动一个网格,Josh的回答非常好,但是它缺乏对多个元素移动的能力。
以下是如何单独移动多个元素
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();
}
public void dragme(object sender, MouseButtonEventArgs e)
{
if (_Move.IsChecked == true)
db.Attach((DependencyObject)sender);
}
//// 鼠标拖动元素行为 db;
private void canvass_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (_Move.IsChecked == true && filmgrid.Visibility == Visibility.Visible)// == true)
{
filmgrid.PreviewMouseDown += new MouseButtonEventHandler(dragme);
}
}
将网格面板放在画布中而不是直接放在窗口中 - 这样它就会有X/Y坐标。
然后,您可以使用自定义附加属性来实现拖放行为,例如此处详细说明的那样(http://www.deepcode.co.uk/archive/2008/11/16/using-attached-properties-to-compose-new-behaviour.aspx)
Canvas.SetLeft
,但性能没有改变。 - SepehrM