0

WPF でいくつかTextBoxes動的に作成し、その後、Canvasそれらを作成したすべての子オブジェクトを見つけに行きました。検索すると、テキスト ボックスの名前を取得できますが、テキスト ボックス内のテキストを変更するにはどうすればよいですか?

私が試してみました:

// oText is the visual object I found when searching for the textbox
oText.Text = "Software" // doesnt work.
oText.SetValue(control.Text) // doesnt work, because there is no .text property

デバッグしても、oTextオブジェクトにカーソルを合わせ、下にスクロールして、Textプロパティが に設定されて"Software"いることを確認できますが、

oText.GetValue(control.width)

この動的に作成されたテキスト ボックスの WPF でテキスト値を読み取るにはどうすればよいでしょうか。

コードは次のとおりです。

XAML でキャンバスを作成します。

 <Canvas x:Name="Can1" Height="700" Width="874">

        </Canvas>

次に、テキストボックスを作成してキャンバスに配置します...

 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 = Canvas.GetLeft(childVisual)
        y = Canvas.GetTop(childVisual)

        A = childVisual.GetValue(Control.ActualHeightProperty)
        B = childVisual.GetValue(Control.ActualWidthProperty)     

        Dim myTextbox As New TextBox
        Dim c As Int16
        myTextbox.Width = B
        myTextbox.Text = "Software"
        myTextbox.Name = "TextB" & i.ToString
        Can1.Children.Add(myTextbox)
        Canvas.SetTop(myTextbox, y + A)
        Canvas.SetLeft(myTextbox, x)
        Canvas.SetZIndex(myTextbox, 0)
next i

次に、メイン ウィンドウのボタンを使用して GetData を呼び出します...

Private Sub GetData(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    Dim iCount As Int16
    Dim oText As Visual
    Dim sTemp As String
    Dim cs = My.Settings.ConnectionString
    Dim oConn = New SqlConnection(cs)
    Dim cmd As New SqlCommand()
    Text1.text = ""
    cmd.Connection = oConn
    Try
        oConn.Open()
        cmd.CommandText = "select top 5 finumber from fiheading "
        Dim myReader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        While myReader.Read()
            iCount += 1
            oText = FindChild(Can1, "TextB" & iCount.ToString)
            'sTemp = oText.GetValue(Control.NameProperty)
            'oText.text = (myReader.GetString(0))
            'oText.SetValue(Control.text)

        End While
        myReader.Close()
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try


    Try
        oConn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try


End Sub
4

1 に答える 1

1

oTextプロパティVisualを持たないとして定義されていますText

それを a に変更し、結果を aにTextBoxキャストすると、正常に動作するはずですFindChildTextBox

Dim oText As TextBox
...

oText = CType(FindChild(Can1, "TextB" & iCount.ToString), TextBox)
于 2012-06-28T19:55:30.467 に答える