HI,
如何在WPF窗口内移动(拖动)网格面板?网格面板没有位置或位置或X和Y坐标属性。我所看到的就是使用鼠标将网格面板从当前位置移动到新位置,以便显示在其下方“埋葬”的控件。
任何指针?
非常感谢。
答案 0 :(得分:13)
以下是一些代码示例,可帮助您入门:
在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();
}
答案 1 :(得分:0)
只需将网格面板放在画布内而不是直接放入窗口中 - 然后将其赋予X / Y坐标。
然后,您可以使用自定义附加属性实现拖放行为,例如详细信息(http://www.deepcode.co.uk/archive/2008/11/16/using-attached-properties-to-compose-new-behaviour.aspx)
答案 2 :(得分:0)
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);
}
}