如何在鼠标移动时更改画布子项的位置?

时间:2017-09-14 11:55:29

标签: c# wpf canvas

我在画布对象“CanvasContain”中有很多UI元素。在鼠标移动时,我想要偏移该画布中的所有UI元素。我尝试使用画布的名称,它工作正常:

foreach(UIElement child in CanvasContain.Children)
{
    if (child != null)
    { 
        Canvas2.Offset -= position - LastMousePosition;
        Canvas3.Offset -= position - LastMousePosition;
    }
}

但是当我尝试使用child.offset时,它无效。如何动态更改偏移?

1 个答案:

答案 0 :(得分:2)

您需要为每个孩子调整Canvas Left和Top属性:

A = [1,3,5,9,11]

u = A[0]
for i in range(1, len(A)):
    if A[i] > u:
            u = A[i]
    if u <= 0:
        Ans = 1

    else:
        for j in range(u ,0, -1):
            if j not in A:
                Ans = j
                print(Ans)
                break
        else:    # <-- attention, this "else" must be aligned with the preceding "for", not the "if"
            continue
        break

注意我放弃了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)); } 的测试,这是不必要的。

相关问题