WPF TreeView Selecteditem。添加子项(VB,net)的问题

时间:2015-04-23 19:50:44

标签: wpf vb.net treeview

这可能有一个非常简单的解决方案,但我对此很新。我正在尝试通过单击按钮将子项添加到选定的treeView项中。代码工作正常,未选择任何内容,但似乎Treeview1.selectedItem没有.Add方法。

Imports System.Windows.Controls.TreeView
Class MainWindow
  Private Sub addNodeButton_Click(sender As Object, e As RoutedEventArgs) Handles addNodeButton.Click
    Dim n As String = "Model"
    If TreeView1.SelectedItem Is Nothing Then
        TreeView1.Items.Add(n)
    Else
        TreeView1.SelectedItem.add("test")
    End If
  End Sub
End Class

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:0)

确定。我知道事情会变得简单。我最初添加的项目为TreeViewItems的Strings insead,因此无法添加子项。固定代码如下:

Private Sub addNodeButton_Click(sender As Object, e As RoutedEventArgs) Handles addNodeButton.Click
    Dim n As New TreeViewItem
    n.Header = "Model"
    If TreeView1.SelectedItem Is Nothing Then
        TreeView1.Items.Add(n)
    Else
        Dim tempitem As New TreeViewItem
        tempitem = TreeView1.SelectedItem
        Dim newitem As New TreeViewItem
        newitem.Header = "test"
        tempitem.Items.Add(newitem)

    End If


End Sub