WPF循环遍历网格子项中的文本框控件

时间:2015-01-16 11:00:21

标签: wpf vb.net foreach textbox

我有一个包含许多子项的大网格作为按钮,标签和网格。层次结构的最后一个网格包含文本框。

我想循环浏览那些文本框,但这不起作用:

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

1 个答案:

答案 0 :(得分:1)

你应该只让TextBox个孩子进行循环:

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