WPF 循环遍历网格子项中的文本框控件
WPF loop through textbox controls in grid children
我有一个大网格,其中包含许多子元素,如按钮、标签和网格。层次结构的最后一个网格包含文本框。
我想遍历这些文本框,但这不起作用:
For Each g1 As Grid In LayoutRoot.Children
If (Row.Children.GetType Is GetType(Grid)) Then
For Each g2 As Grid In g1.Children
For Each g3 As Grid In g2.Children
For Each txtbox As TextBox In g3.Children
'Some Code
Next
Next
Next
End If
Next
你应该只得到 TextBox
键入 children 来遍历它们:
g3.Children.OfType(Of TextBox)()
完整代码:
For Each g1 In LayoutRoot.Children.OfType(Of Grid)() //change
'If (TypeOf(g1.children) Is grid Then
For Each g2 As Grid In g1.Children
For Each g3 As Grid In g2.Children
For Each txtbox As TextBox In g3.Children.OfType(Of TextBox)() //change
'Some Code
Next
Next
Next
'End If
Next
我有一个大网格,其中包含许多子元素,如按钮、标签和网格。层次结构的最后一个网格包含文本框。
我想遍历这些文本框,但这不起作用:
For Each g1 As Grid In LayoutRoot.Children
If (Row.Children.GetType Is GetType(Grid)) Then
For Each g2 As Grid In g1.Children
For Each g3 As Grid In g2.Children
For Each txtbox As TextBox In g3.Children
'Some Code
Next
Next
Next
End If
Next
你应该只得到 TextBox
键入 children 来遍历它们:
g3.Children.OfType(Of TextBox)()
完整代码:
For Each g1 In LayoutRoot.Children.OfType(Of Grid)() //change
'If (TypeOf(g1.children) Is grid Then
For Each g2 As Grid In g1.Children
For Each g3 As Grid In g2.Children
For Each txtbox As TextBox In g3.Children.OfType(Of TextBox)() //change
'Some Code
Next
Next
Next
'End If
Next