这可能有一个非常简单的解决方案,但我对此很新。我正在尝试通过单击按钮将子项添加到选定的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
任何帮助表示感谢。
答案 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