2017-09-14 28 views
0

我在畫布對象「CanvasContain」中有很多UI元素。在鼠標移動時,我想要抵消該畫布中的所有UI元素。我與畫布的名字嘗試,這是工作的罰款:如何在鼠標移動時更改畫布小孩的位置?

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

但是,當我嘗試用child.offset它無法正常工作。我怎樣才能動態地改變偏移量?

+0

順便說一句,我使用ZoomableCanvas zomming和平移 – srinivas

回答

2

您需要調整畫布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這是沒有必要的。

相關問題