我想從一個字符串,其中字符串等於TextBox.Name得到我的WPF窗體對象文本框在VB中的WPF:如何從字符串的窗體中獲取文本框?
我的XAML看起來像這樣:
<Window>
<DockPanel>
<TabItem Header="3">
<Grid>
<TextBox x:Name="textBox1"/>
<TextBox x:Name="textBox2"/>
</Grid>
</TabItem>
</Dockpanel>
</Window>
在VB中我做了這個功能這是行不通的
Private Function getTextboxOutName(ByVal parent As DependencyObject, ByVal textBoxName As String) As Object
Dim element As Object
Dim aantalElementen As Integer = VisualTreeHelper.GetChildrenCount(parent)
If aantalElementen > 0 Then
For n As Integer = 0 To aantalElementen - 1
Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, n)
element = child
If element.Name = textBoxName Then
Return element
End If
Next
End If
Return Nothing
End Function
我在做什麼錯?
在此先感謝
爲什麼你不使用文本框的名字來訪問它的代碼? – Filip
請你能描述一個你需要使用這種功能的場景嗎? – Fabio
我有8個文本框和8個字符串,我想在一個文本框中隨機放置1個字符串。所以隨機詮釋我會給我textBox(我)。 –