Take the 2-minute tour ×

When I create a dynamic image on a canvas, and then try ti find it with visualtreehelper.getchild, it finds the image, but the offsets are 0.

It works if I use xaml to make the image, but not if I dynamically create it. Is there something you have to do extra if you create an image dynamically vs. in xaml to make visualtreehelper.getchild find the offsets?

 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

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

    '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
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

OK I found the answer after hours of research. When dynamically creating images on a canvas, after you are done, you need to call:


in my case it is: Can1.UpdateLayout()

Now I am getting the offsets returned to me correctly.

share|improve this answer
add comment

Your Answer


