Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET-用于WPF控件中的每个_Wpf_Vb.net_Xaml - Fatal编程技术网

VB.NET-用于WPF控件中的每个

VB.NET-用于WPF控件中的每个,wpf,vb.net,xaml,Wpf,Vb.net,Xaml,我想检查WPF表单中的一些子元素。在Windows窗体中,使用(例如)非常简单: 如何在XAML.VB文件中的WPF中实现这一点?我得到了一个列表框,其中包含一些我想分析的子元素 格里茨,谢谢你的想法 您必须使用VisualTreeHelper.GetChild方法,并递归调用该方法来降低树。如果知道要查找的控件的名称,则可以使用FrameworkElement.FindName方法。中提供了大量选项和示例代码,提供了更多信息。您必须使用VisualTreeHelper.GetChild方法,并

我想检查WPF表单中的一些子元素。在Windows窗体中,使用(例如)非常简单:

如何在XAML.VB文件中的WPF中实现这一点?我得到了一个列表框,其中包含一些我想分析的子元素


格里茨,谢谢你的想法

您必须使用
VisualTreeHelper.GetChild
方法,并递归调用该方法来降低树。如果知道要查找的控件的名称,则可以使用
FrameworkElement.FindName
方法。中提供了大量选项和示例代码,提供了更多信息。

您必须使用
VisualTreeHelper.GetChild
方法,并递归调用该方法来降低树。如果知道要查找的控件的名称,则可以使用
FrameworkElement.FindName
方法。中有大量选项和示例代码的更多信息。

视情况而定。假设有一个列表框定义为:

<ListBox x:Name="listBox">
    <ListBoxItem>Item1</ListBoxItem>
    <ListBoxItem>Item2</ListBoxItem>
    <ListBoxItem>Item3</ListBoxItem>
</ListBox>
但是,如果ListBox中的项是通过ItemsSource属性定义的,则这将不起作用。因此,如果项目定义如下:

    Dim itemsSource As New List(Of Object)
    itemsSource.Add("Object1")
    itemsSource.Add(200D)
    itemsSource.Add(DateTime.Now)
    Me.listBox.ItemsSource = itemsSource
您必须使用ItemContainerGenerator来获取实际的ListBoxItem

    For Each x In Me.listBox.Items
        Dim item As ListBoxItem = DirectCast(Me.listBox.ItemContainerGenerator.ContainerFromItem(x), ListBoxItem)
        Console.WriteLine(item.Content.ToString())
    Next
现在,上面的示例特定于ListBox控件。如果您想获得给定控件(例如窗口)的整个可视树中的所有项,则必须使用VisualTreeHelper.GetChild(如Brian所建议的)

不管怎样,这里有一个我写的小样本,可能会帮助你。它包含我上面描述的所有内容

XAML:


视情况而定。假设有一个列表框定义为:

<ListBox x:Name="listBox">
    <ListBoxItem>Item1</ListBoxItem>
    <ListBoxItem>Item2</ListBoxItem>
    <ListBoxItem>Item3</ListBoxItem>
</ListBox>
但是,如果ListBox中的项是通过ItemsSource属性定义的,则这将不起作用。因此,如果项目定义如下:

    Dim itemsSource As New List(Of Object)
    itemsSource.Add("Object1")
    itemsSource.Add(200D)
    itemsSource.Add(DateTime.Now)
    Me.listBox.ItemsSource = itemsSource
您必须使用ItemContainerGenerator来获取实际的ListBoxItem

    For Each x In Me.listBox.Items
        Dim item As ListBoxItem = DirectCast(Me.listBox.ItemContainerGenerator.ContainerFromItem(x), ListBoxItem)
        Console.WriteLine(item.Content.ToString())
    Next
现在,上面的示例特定于ListBox控件。如果您想获得给定控件(例如窗口)的整个可视树中的所有项,则必须使用VisualTreeHelper.GetChild(如Brian所建议的)

不管怎样,这里有一个我写的小样本,可能会帮助你。它包含我上面描述的所有内容

XAML:

Class MainWindow 


    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        'Get all ListBoxItems
        For Each x As ListBoxItem In Me.listBox.Items
            Console.WriteLine(x.Content.ToString())
        Next

        'Get all items in the StackPanel
        For Each y As UIElement In Me.sp.Children
            Console.WriteLine(y.ToString())
        Next

    End Sub

    Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        'Set an ItemsSource for the ListBox

        Me.listBox.Items.Clear()

        Dim itemsSource As New List(Of Object)
        itemsSource.Add("Object1")
        itemsSource.Add(200D)
        itemsSource.Add(DateTime.Now)
        Me.listBox.ItemsSource = itemsSource
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

        'Print the items within the ListBox
        For Each x In Me.listBox.Items
            Dim item As ListBoxItem = DirectCast(Me.listBox.ItemContainerGenerator.ContainerFromItem(x), ListBoxItem)
            Console.WriteLine(item.Content.ToString())
        Next

    End Sub

    Private Sub btnPrintVisualTree_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        PrintVisualTreeRecursive(Me)
    End Sub

    Private Sub PrintVisualTreeRecursive(ByVal parent As DependencyObject)

        Console.WriteLine(parent.ToString())

        Dim count As Integer = VisualTreeHelper.GetChildrenCount(parent)
        If count > 0 Then
            For n As Integer = 0 To count - 1
                Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, n)
                PrintVisualTreeRecursive(child)
            Next
        End If
    End Sub

End Class