Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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
Wpf MessageBox必须显示元素名称而不是元素类型_Wpf_Vb.net_Xaml - Fatal编程技术网

Wpf MessageBox必须显示元素名称而不是元素类型

Wpf MessageBox必须显示元素名称而不是元素类型,wpf,vb.net,xaml,Wpf,Vb.net,Xaml,xaml 上面的代码非常有效 当您运行上述代码并单击按钮1时,您将看到消息框显示您System.Windows.Controls.StackPanel “我想要MessageBox”现在显示我的堆栈面板名称,您在对对象类型进行强制转换时所做的一切,它没有名称属性。您需要做的是检查对象是否属于StackPanel类型,然后强制转换为该类型以访问其Name属性 Private Sub Button1_Click(sender As Object, e As RoutedEventArgs)

xaml

上面的代码非常有效

当您运行上述代码并单击按钮1时,您将看到消息框显示您System.Windows.Controls.StackPanel


“我想要MessageBox”现在显示我的堆栈面板名称,您在对对象类型进行强制转换时所做的一切,它没有
名称
属性。您需要做的是检查对象是否属于
StackPanel
类型,然后强制转换为该类型以访问其Name属性

    Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
    For Each obj As Object In LogicalTreeHelper.GetChildren(CType(ListView1.SelectedItem, DependencyObject))
        MessageBox.Show(obj.ToString)
    Next
    End Sub

必须将元素转换为具有名称的类型。在XAML中,您可以获得名称,因为它是UIElement的依赖属性,所以还有其他选项<代码>对象没有
名称
属性,它也不是
依赖对象
。。。您需要迭代一个类型,或者尝试将obj转换为您期望的类型。
    Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
    For Each obj As Object In LogicalTreeHelper.GetChildren(CType(ListView1.SelectedItem, DependencyObject))
        MessageBox.Show(obj.ToString)
    Next
    End Sub
    For Each obj As Object In LogicalTreeHelper.GetChildren(CType(ListView1.SelectedItem, DependencyObject))
        If TypeOf obj Is StackPanel Then
            Dim stackPanel As StackPanel = CType(obj, StackPanel)
            MessageBox.Show(stackPanel.Name)
        End If
    Next