Skip to content

Commit 94c76f4

Browse files
authored
Merge pull request #508 from Nukepayload2/patch-1
Add VB code example to tree-view.md
2 parents 2ff7fec + 7a64321 commit 94c76f4

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

windows-apps-src/design/controls-and-patterns/tree-view.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ pm-contact: predavid
1010
design-contact: ksulliv
1111
dev-contact: joyate
1212
doc-status: Published
13+
dev_langs:
14+
- csharp
15+
- vb
1316
---
1417
# TreeView
1518

@@ -80,6 +83,18 @@ private void InitializeTreeView()
8083
}
8184
```
8285

86+
```vb
87+
Private Sub InitializeTreeView()
88+
Dim rootNode As New TreeViewNode With {.Content = "Flavors", .IsExpanded = True}
89+
With rootNode.Children
90+
.Add(New TreeViewNode With {.Content = "Vanilla"})
91+
.Add(New TreeViewNode With {.Content = "Strawberry"})
92+
.Add(New TreeViewNode With {.Content = "Chocolate"})
93+
End With
94+
sampleTreeView.RootNodes.Add(rootNode)
95+
End Sub
96+
```
97+
8398
These APIs are available for managing the data hierarchy of your tree view.
8499

85100
| **[TreeView](/uwp/api/windows.ui.xaml.controls.treeview)** | |
@@ -108,6 +123,11 @@ TreeViewNode pictureNode = new TreeViewNode();
108123
pictureNode.Content = picturesFolder;
109124
```
110125

126+
```vb
127+
Dim picturesFolder As StorageFolder = KnownFolders.PicturesLibrary
128+
Dim pictureNode As New TreeViewNode With {.Content = picturesFolder}
129+
```
130+
111131
You can provide a [DataTemplate](/uwp/api/windows.ui.xaml.datatemplate) to specify how the data item is displayed in the tree view.
112132

113133
> [!NOTE]
@@ -151,6 +171,14 @@ private void SampleTreeView_Expanding(TreeView sender, TreeViewExpandingEventArg
151171
}
152172
```
153173

174+
```vb
175+
Private Sub SampleTreeView_Expanding(sender As TreeView, args As TreeViewExpandingEventArgs)
176+
If args.Node.HasUnrealizedChildren Then
177+
FillTreeNode(args.Node)
178+
End If
179+
End Sub
180+
```
181+
154182
It's not required, but you might want to also handle the [Collapsed](/uwp/api/windows.ui.xaml.controls.treeview.collapsed) event and remove the child nodes when the parent node is closed. This can be important if your tree view has many nodes, or if the node data uses a lot of resources. You should consider the performance impact of filling a node each time it's opened versus leaving the children on a closed node. The best option will depend on your app.
155183

156184
Here's an example of a handler for the Collapsed event.
@@ -163,6 +191,13 @@ private void SampleTreeView_Collapsed(TreeView sender, TreeViewCollapsedEventArg
163191
}
164192
```
165193

194+
```vb
195+
Private Sub SampleTreeView_Collapsed(sender As TreeView, args As TreeViewCollapsedEventArgs)
196+
args.Node.Children.Clear()
197+
args.Node.HasUnrealizedChildren = True
198+
End Sub
199+
```
200+
166201
### Invoking an item
167202

168203
A user can invoke an action (treating the item like a button) instead of selecting the item. You handle the [ItemInvoked](/uwp/api/windows.ui.xaml.controls.treeview.iteminvoked) event to respond to this user interaction.
@@ -194,6 +229,21 @@ private void SampleTreeView_ItemInvoked(TreeView sender, TreeViewItemInvokedEven
194229
}
195230
```
196231

232+
```vb
233+
Private Sub SampleTreeView_ItemInvoked(sender As TreeView, args As TreeViewItemInvokedEventArgs)
234+
Dim node = TryCast(args.InvokedItem, TreeViewNode)
235+
Dim item = TryCast(node.Content, IStorageItem)
236+
If item IsNot Nothing Then
237+
FileNameTextBlock.Text = item.Name
238+
FilePathTextBlock.Text = item.Path
239+
TreeDepthTextBlock.Text = node.Depth.ToString()
240+
If TypeOf node.Content Is StorageFolder Then
241+
node.IsExpanded = Not node.IsExpanded
242+
End If
243+
End If
244+
End Sub
245+
```
246+
197247
### Item selection
198248

199249
The TreeView control supports both single-selection and multi-selection. By default, selection of nodes is turned off, but you can set the [TreeView.SelectionMode](/uwp/api/windows.ui.xaml.controls.treeview.selectionmode) property to allow selection of nodes. The [TreeViewSelectionMode](/uwp/api/windows.ui.xaml.controls.treeviewselectionmode) values are **None**, **Single**, and **Multiple**.
@@ -304,6 +354,26 @@ private void SelectAllButton_Click(object sender, RoutedEventArgs e)
304354
}
305355
```
306356

357+
```vb
358+
Private Sub OrderButton_Click(sender As Object, e As RoutedEventArgs)
359+
FlavorList.Text = String.Empty
360+
ToppingList.Text = String.Empty
361+
For Each node As TreeViewNode In DessertTree.SelectedNodes
362+
If node.Parent.Content?.ToString() = "Flavors" Then
363+
FlavorList.Text += node.Content & "; "
364+
ElseIf node.HasChildren = False Then
365+
ToppingList.Text += node.Content & "; "
366+
End If
367+
Next
368+
End Sub
369+
370+
Private Sub SelectAllButton_Click(sender As Object, e As RoutedEventArgs)
371+
If DessertTree.SelectionMode = TreeViewSelectionMode.Multiple Then
372+
DessertTree.SelectAll()
373+
End If
374+
End Sub
375+
```
376+
307377
### Pictures and Music Library tree view
308378

309379
This example shows how to create a tree view that shows the contents and structure of the users Pictures and Music libraries. The number of items can't be known ahead of time, so each node is filled when it's expanded, and emptied when it's collapsed.
@@ -505,6 +575,104 @@ private void RefreshButton_Click(object sender, RoutedEventArgs e)
505575
}
506576
```
507577

578+
```vb
579+
Public Sub New()
580+
InitializeComponent()
581+
InitializeTreeView()
582+
End Sub
583+
584+
Private Sub InitializeTreeView()
585+
' A TreeView can have more than 1 root node. The Pictures library
586+
' and the Music library will each be a root node in the tree.
587+
' Get Pictures library.
588+
Dim picturesFolder As StorageFolder = KnownFolders.PicturesLibrary
589+
Dim pictureNode As New TreeViewNode With {
590+
.Content = picturesFolder,
591+
.IsExpanded = True,
592+
.HasUnrealizedChildren = True
593+
}
594+
sampleTreeView.RootNodes.Add(pictureNode)
595+
FillTreeNode(pictureNode)
596+
597+
' Get Music library.
598+
Dim musicFolder As StorageFolder = KnownFolders.MusicLibrary
599+
Dim musicNode As New TreeViewNode With {
600+
.Content = musicFolder,
601+
.IsExpanded = True,
602+
.HasUnrealizedChildren = True
603+
}
604+
sampleTreeView.RootNodes.Add(musicNode)
605+
FillTreeNode(musicNode)
606+
End Sub
607+
608+
Private Async Sub FillTreeNode(node As TreeViewNode)
609+
' Get the contents of the folder represented by the current tree node.
610+
' Add each item as a new child node of the node that's being expanded.
611+
612+
' Only process the node if it's a folder and has unrealized children.
613+
Dim folder As StorageFolder = Nothing
614+
If TypeOf node.Content Is StorageFolder AndAlso node.HasUnrealizedChildren Then
615+
folder = TryCast(node.Content, StorageFolder)
616+
Else
617+
' The node isn't a folder, or it's already been filled.
618+
Return
619+
End If
620+
621+
Dim itemsList As IReadOnlyList(Of IStorageItem) = Await folder.GetItemsAsync()
622+
If itemsList.Count = 0 Then
623+
' The item is a folder, but it's empty. Leave HasUnrealizedChildren = true so
624+
' that the chevron appears, but don't try to process children that aren't there.
625+
Return
626+
End If
627+
628+
For Each item In itemsList
629+
Dim newNode As New TreeViewNode With {
630+
.Content = item
631+
}
632+
If TypeOf item Is StorageFolder Then
633+
' If the item is a folder, set HasUnrealizedChildren to True.
634+
' This makes the collapsed chevron show up.
635+
newNode.HasUnrealizedChildren = True
636+
Else
637+
' Item is StorageFile. No processing needed for this scenario.
638+
End If
639+
node.Children.Add(newNode)
640+
Next
641+
642+
' Children were just added to this node, so set HasUnrealizedChildren to False.
643+
node.HasUnrealizedChildren = False
644+
End Sub
645+
646+
Private Sub SampleTreeView_Expanding(sender As TreeView, args As TreeViewExpandingEventArgs)
647+
If args.Node.HasUnrealizedChildren Then
648+
FillTreeNode(args.Node)
649+
End If
650+
End Sub
651+
652+
Private Sub SampleTreeView_Collapsed(sender As TreeView, args As TreeViewCollapsedEventArgs)
653+
args.Node.Children.Clear()
654+
args.Node.HasUnrealizedChildren = True
655+
End Sub
656+
657+
Private Sub SampleTreeView_ItemInvoked(sender As TreeView, args As TreeViewItemInvokedEventArgs)
658+
Dim node = TryCast(args.InvokedItem, TreeViewNode)
659+
Dim item = TryCast(node.Content, IStorageItem)
660+
If item IsNot Nothing Then
661+
FileNameTextBlock.Text = item.Name
662+
FilePathTextBlock.Text = item.Path
663+
TreeDepthTextBlock.Text = node.Depth.ToString()
664+
If TypeOf node.Content Is StorageFolder Then
665+
node.IsExpanded = Not node.IsExpanded
666+
End If
667+
End If
668+
End Sub
669+
670+
Private Sub RefreshButton_Click(sender As Object, e As RoutedEventArgs)
671+
sampleTreeView.RootNodes.Clear()
672+
InitializeTreeView()
673+
End Sub
674+
```
675+
508676
## Related articles
509677

510678
- [TreeView class](https://docs.microsoft.com/uwp/api/windows.ui.xaml.controls.treeview)

0 commit comments

Comments
 (0)