当我在画布上创建动态图像,然后尝试使用visualtreehelper.getchild找到它时,它会找到图像,但偏移量为0。
如果我使用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 :(得分:0)
好的,经过数小时的研究,我找到了答案。 在画布上动态创建图像时,完成后,需要调用:
<object>.UpdateLayout()
就我而言: Can1.UpdateLayout()
现在我正确地将偏移量退回给我。