VB.net - WPF - Help with treeview
-
I am new to VB.net and WPF. and i need some help. When i select a item in the combobox. then i will have that Directory i have selected in the combobox. add to the listbox or treeview (as a treeview with folders/subfolders/... and files) edit: My problem is i apparently can't use .Nodes in my VB.net code. And my events (afterselected) is useless.. I can use it in VB.net(winforms) but not as WPF (Class Library)
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.InputPublic Class VerticalPaneExample
Dim root = "c:\temp\"
Dim matroot As String = ""
Dim newtoolsti As String = ""
Dim enodetext As String = ""Private Sub VerticalPaneExample\_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded Dim folders() As String = IO.Directory.GetDirectories(root) For Each folder As String In folders Dim clean As String clean = folder.Replace(root, "") Me.matpicker.Items.Add(clean) Next End Sub Private Sub matpicker\_AfterSelect(sender As Object, e As RoutedEventArgs) Handles matpicker.AfterSelect Me.matpicker.SelectedItem = matroot For Each direct As String In System.IO.Directory.GetDirectories(matroot) Dim dir As String = System.IO.Path.GetFileNameWithoutExtension(direct) Dim newNode = viewptffiles.Nodes.Add(dir, dir) RecurseChildFolders(direct, newNode) Try For Each file As String In System.IO.Directory.GetFiles(direct, "\*.\*") newNode.Nodes.Add(System.IO.Path.GetFileName(file)) Next Catch ex As Exception End Try Next Private Sub RecurseChildFolders(directory As String, parent As TreeNode) For Each direct As String In System.IO.Directory.GetDirectories(directory) Dim dir As String = System.IO.Path.GetFileNameWithoutExtension(direct) Dim child = parent.Nodes.Add(dir, dir) RecurseChildFolders(direct, child) Try For Each file As String In System.IO.Directory.GetFiles(direct, "\*.\*") child.Nodes.Add(System.IO.Path.GetFileName(file)) Next Catch ex As Exception End Try Next End Sub
End Class
My xaml file look like this:
-
I am new to VB.net and WPF. and i need some help. When i select a item in the combobox. then i will have that Directory i have selected in the combobox. add to the listbox or treeview (as a treeview with folders/subfolders/... and files) edit: My problem is i apparently can't use .Nodes in my VB.net code. And my events (afterselected) is useless.. I can use it in VB.net(winforms) but not as WPF (Class Library)
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.InputPublic Class VerticalPaneExample
Dim root = "c:\temp\"
Dim matroot As String = ""
Dim newtoolsti As String = ""
Dim enodetext As String = ""Private Sub VerticalPaneExample\_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded Dim folders() As String = IO.Directory.GetDirectories(root) For Each folder As String In folders Dim clean As String clean = folder.Replace(root, "") Me.matpicker.Items.Add(clean) Next End Sub Private Sub matpicker\_AfterSelect(sender As Object, e As RoutedEventArgs) Handles matpicker.AfterSelect Me.matpicker.SelectedItem = matroot For Each direct As String In System.IO.Directory.GetDirectories(matroot) Dim dir As String = System.IO.Path.GetFileNameWithoutExtension(direct) Dim newNode = viewptffiles.Nodes.Add(dir, dir) RecurseChildFolders(direct, newNode) Try For Each file As String In System.IO.Directory.GetFiles(direct, "\*.\*") newNode.Nodes.Add(System.IO.Path.GetFileName(file)) Next Catch ex As Exception End Try Next Private Sub RecurseChildFolders(directory As String, parent As TreeNode) For Each direct As String In System.IO.Directory.GetDirectories(directory) Dim dir As String = System.IO.Path.GetFileNameWithoutExtension(direct) Dim child = parent.Nodes.Add(dir, dir) RecurseChildFolders(direct, child) Try For Each file As String In System.IO.Directory.GetFiles(direct, "\*.\*") child.Nodes.Add(System.IO.Path.GetFileName(file)) Next Catch ex As Exception End Try Next End Sub
End Class
My xaml file look like this:
You've told us what you want to do, and shown your code, but you forgot to tell us what the problem is or where you're stuck.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
You've told us what you want to do, and shown your code, but you forgot to tell us what the problem is or where you're stuck.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Your WPF code is using a
ListBox
, not a tree-view control. TreeView, data binding and multiple templates - The complete WPF tutorial[^] TreeView Overview - WPF .NET Framework | Microsoft Docs[^] How to: Create Simple or Complex TreeViews - WPF .NET Framework | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer