2012-09-26 91 views
0

我想查找UIElementLeft,Top,Right,Bottom。我嘗試過但沒有結果。如何獲取WPF中UIElement的左,上,右,底點?

任何人都可以在這裏有任何想法如何得到這些?

其實,我正在WPF中創建一個自定義面板。

我不想從畫布繼承。

Size elemSize = this.ElementBeingDragged.DesiredSize; 
// Get the element's offsets from the four sides of the Panel. 
Vector v= VisualTreeHelper.GetOffset(this.ElementBeingDragged); 
double left = v.X; 
double right = v.X + elemSize.Width; 
double top = v.Y; 
double bottom = v.Y+elemSize.Height; 
+0

我已經放置我的代碼 –

+0

您是否嘗試過TranslatePoint或TransformToAncestor? – Josh

回答

0

您可以使用此代碼嘗試 - 基於TransformToAncestor

private Point GetPosition(Visual item) 
{ 
    var transformToAncestor = item.TransformToAncestor(MyForm); 

    var position = transformToAncestor.Transform(new Point(0, 0)); 

    return position; 
} 
1

嘗試

Point position = ElementBeingDragged.TranslatePoint(new Point(0, 0), UIElementRelativeTo); 

例如獲取點相對於包含窗口:

Point position = ElementBeingDragged.TranslatePoint(new Point(0, 0), Window.GetWindow(ElementBeingDragged)); 
相關問題