2012-06-21 56 views
0

的偏移發現當我創建一個畫布上的動態圖像,然後嘗試用TI發現visualtreehelper.getchild它,它找到的圖像,但偏移量是0圖像沒有被visualtreehelper.getchild

它適用於如果我使用xaml來製作圖像,但不是如果我動態創建它。如果您在xaml中動態創建圖像以使visualtreehelper.getchild找到偏移量,您是否需要額外執行某些操作?

Dim iCount As Int16 = 0 

    'Put Tables on Canvas 
    For iCount = 0 To 5 
     Dim tbl As New Image 

     tbl.Source = New BitmapImage(New Uri("C:/Images/woodtable4.jpg", UriKind.Absolute)) 

     tbl.Width = 50 
     tbl.Height = 50 

     tbl.Name = "Tbl" & iCount.ToString 
     Can1.Children.Add(tbl) 

     Canvas.SetTop(tbl, 50) 
     Canvas.SetLeft(tbl, (100 * iCount)) 
     'Can1.RegisterName(tbl.Name, tbl) 
    Next 

    'Get Child Objects 

    Dim x As Int16 
    Dim y As Int16 
    For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(Can1) - 1 
     ' Retrieve child visual at specified index value. 
     Dim childVisual As Visual = CType(VisualTreeHelper.GetChild(Can1, i), Visual) 

     ' Return the offset vector for the TextBlock object. 
     Dim vector As Vector = VisualTreeHelper.GetOffset(childVisual) 
     ' Convert the vector to a point value. 
     Dim currentPoint As New Point(VisualOffset.X, VisualOffset.Y) 

     x = currentPoint.X 
     y = currentPoint.Y 
    Next i 

回答

0

好的我在經過幾個小時的研究後找到了答案。 如果動態地創建在畫布上的圖像,在完成後,你需要調用:

<object>.UpdateLayout() 
在我的情況

是: Can1.UpdateLayout()

現在我得到的偏移量返回我正確。