如何在鼠标移动时更改画布小孩的位置?
问题描述:
我在画布对象“CanvasContain”中有很多UI元素。在鼠标移动时,我想要抵消该画布中的所有UI元素。我与画布的名字尝试,这是工作的罚款:如何在鼠标移动时更改画布小孩的位置?
foreach(UIElement child in CanvasContain.Children)
{
if (child != null)
{
Canvas2.Offset -= position - LastMousePosition;
Canvas3.Offset -= position - LastMousePosition;
}
}
但是,当我尝试用child.offset
它无法正常工作。我怎样才能动态地改变偏移量?
答
您需要调整画布Left和Top属性为每个孩子:
foreach(UIElement child in CanvasContain.Children)
{
double x = Canvas.GetLeft(child);
double y = Canvas.GetTop(child);
Canvas.SetLeft(child, x - (position.X - LastMousePosition.X));
Canvas.SetTop (child, y - (position.Y - LastMousePosition.Y));
}
注意我放弃了测试child != null
这是没有必要的。
顺便说一句,我使用ZoomableCanvas zomming和平移 – srinivas