在 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