0

キャンバス上に動的画像を作成し、visualtreehelper.getchildでそれを見つけようとすると、画像は見つかりますが、オフセットは0です。

xamlを使用してイメージを作成する場合は機能しますが、動的に作成する場合は機能しません。visualtreehelper.getchildにオフセットを見つけさせるために、xamlで動的に画像を作成する場合と比較して、追加で行う必要があることはありますか?

 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
4

1 に答える 1

0

OK何時間もの調査の結果、答えが見つかりました。キャンバス上に画像を動的に作成する場合は、完了後に次のコマンドを呼び出す必要があります。

<object>.UpdateLayout()

私の場合は次のとおりです。Can1.UpdateLayout()

これで、オフセットが正しく返されます。

于 2012-06-21T16:03:46.830 に答える