Skip to content

Commit 4fa7cfb

Browse files
authored
add SelectionChanged info (MicrosoftDocs#3874)
1 parent ad4df3e commit 4fa7cfb

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

hub/apps/design/controls/tree-view.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,32 @@ If your tree view has unrealized nodes, they are not taken into account for sele
479479

480480
#### SelectedItem/SelectedItems
481481

482-
Starting in WinUI 2.2, TreeView has the [SelectedItem](/uwp/api/microsoft.ui.xaml.controls.treeview.selecteditem) and [SelectedItems](/uwp/api/microsoft.ui.xaml.controls.treeview.selecteditems) properties. You can use these properties to get the content of selected nodes directly. If multiple selection is enabled, SelectedItem contains the first item in the SelectedItems collection.
482+
TreeView has the [SelectedItem](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.treeview.selecteditem) and [SelectedItems](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.treeview.selecteditems) properties. You can use these properties to get the content of selected nodes directly. If multiple selection is enabled, SelectedItem contains the first item in the SelectedItems collection.
483+
484+
#### SelectionChanged
485+
486+
You can handle the [SelectionChanged](/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.treeview.selectionchanged) event to respond when the collection of selected items changes, either programmatically or through user interaction.
487+
488+
```xaml
489+
<TreeView ItemsSource="{x:Bind Folders}"
490+
SelectionMode="Multiple"
491+
SelectionChanged="TreeView_SelectionChanged"/>
492+
```
493+
494+
```csharp
495+
public void TreeView_SelectionChanged(TreeView sender, TreeViewSelectionChangedEventArgs args)
496+
{
497+
foreach (object item in args.RemovedItems)
498+
{
499+
this.SelectedFolders.Remove((Folder)item);
500+
}
501+
502+
foreach (object item in args.AddedItems)
503+
{
504+
this.SelectedFolders.Add((Folder)item);
505+
}
506+
}
507+
```
483508

484509
## Code examples
485510

0 commit comments

Comments
 (0)