-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TreeView selection API spec #31
Open
kaiguo
wants to merge
2
commits into
master
Choose a base branch
from
user/kaiguo/treeview-selection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Background | ||
|
||
The Xaml [TreeView](https://docs.microsoft.com/uwp/api/Microsoft.UI.Xaml.Controls.TreeView) control shows a list of hierarchical data, similar to a ListView. | ||
|
||
When TreeView was first released, you populated it using the [RootNode property](https://docs.microsoft.com/uwp/api/Microsoft.UI.Xaml.Controls.TreeView.RootNodes), giving it a tree of nodes, each node referencing a data item. There was negative feedback about this nodes approach, though, from developers preferring a more data binding friendly pattern along the lines of [ListView.ItemsSource](https://docs.microsoft.com/uwp/api/Windows.UI.Xaml.Controls.ItemsControl.ItemsSource). | ||
|
||
So a [TreeView.ItemsSource](https://docs.microsoft.com/uwp/api/Microsoft.UI.Xaml.Controls.TreeView.ItemsSource) property was added, and now you can use either the nodes approach or the ItemsSource approach. But unlike ListView, TreeView doesn't have the corresponding SelectedItem and SelectedItems properties for single- and multi-select. Those are being added in this spec. | ||
|
||
Also being added in this spec, for the case where the RootNode is being used instead of ItemsSource, is an API to select a single node. That is, there is a [SelectedNodes](https://docs.microsoft.com/uwp/api/Microsoft.UI.Xaml.Controls.TreeView.SelectedNodes) property today, and this spec is adding a singular SelectedNode property. | ||
|
||
Related GitHub issues: | ||
https://github.com/microsoft/microsoft-ui-xaml/issues/197 | ||
https://github.com/microsoft/microsoft-ui-xaml/issues/124 | ||
https://github.com/microsoft/microsoft-ui-xaml/issues/386 | ||
|
||
# Description | ||
|
||
Extend TreeView selection API sets to make selections easier. | ||
|
||
- Add `SelectedNode`: get/set the selected TreeViewNode for single selection | ||
- Add `SelectedItem`: get/set the selected item for single selection | ||
- Add `SelectedItems`: get the SelectedItems collection. | ||
- Fix existing `SelectedNodes` property: Currently this only works in multi-select mode ([SelectionMode](https://docs.microsoft.com/uwp/api/Microsoft.UI.Xaml.Controls.TreeView.SelectionMode) set to `Multiple`). It will now also work in single selection mode (consistent with ListView). | ||
|
||
# Examples | ||
|
||
## TreeView.SelectedNode | ||
Ouput the depth of the selected tree view node. | ||
```C# | ||
TreeViewNode selectedNode = treeView.SelectedNode; | ||
if (selectedNode != null) | ||
{ | ||
Debug.WriteLine($"Selected node depth: {selectedNode.Depth}"); | ||
} | ||
``` | ||
|
||
## TreeView.SelectedItem | ||
Bind the TreeView control to a View Model with the data source and the currently selected item. | ||
```Xaml | ||
<TreeView ItemsSource="{x:Bind Descendants}" | ||
SelectedItem="{x:Bind CurrentDescendant, Mode=TwoWay}" /> | ||
``` | ||
|
||
## TreeView.SelectedItems | ||
```C# | ||
void AddToSelection(TreeView treeView, object item) | ||
{ | ||
IList<Object> selectedItems = treeView.SelectedItems; | ||
selectedItems.Add(item); | ||
} | ||
``` | ||
|
||
# API Details | ||
|
||
``` | ||
namespace Microsoft.UI.Xaml.Controls | ||
{ | ||
[webhosthidden] | ||
unsealed runtimeclass TreeView : Windows.UI.Xaml.Controls.Control | ||
{ | ||
... | ||
|
||
TreeViewNode SelectedNode{ get; set; }; | ||
Object SelectedItem{ get; set; }; | ||
Windows.Foundation.Collections.IVector<Object> SelectedItems{ get; }; | ||
|
||
... | ||
} | ||
} | ||
``` | ||
|
||
# Remarks | ||
|
||
SelectedItem(s) returns Object(collection of objects), it will be the ItemsSource type object when using databinding, or TreeViewNode when not using databinding. | ||
|
||
Prior to WinUI 2.2, the [TreeView.SelectedNodes](https://docs.microsoft.com/uwp/api/Microsoft.UI.Xaml.Controls.TreeView.SelectedNodes) property can only be used when the [SelectionMode](https://docs.microsoft.com/uwp/api/Microsoft.UI.Xaml.Controls.TreeView.SelectionMode) is set to `Multiple`. | ||
|
||
# API Notes | ||
|
||
## Class: TreeView | ||
| Member Name | Description | | ||
|:- |:--| | ||
| SelectedNode | Gets or sets the selected TreeViewNode. | | ||
| SelectedNodes | The collection of nodes that are selected in the tree. The default is an empty collection. | | ||
| SelectedItem | Gets or sets the selected item. | | ||
| SelectedItems | The collection of items that are selected in the tree. The default is an empty collection. | |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason that SelectedItems cannot be set via binding? As SelectedItem can be set by two-way binding, it seems to make sense to me to give the same functionality to SelectedItems as well.
<TreeView ItemsSource="{x:Bind Descendants}" SelectedItems="{x:Bind CurrentDescendants, Mode=TwoWay}" />
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This matches ListView.SelectedItems, but I realize that doesn't answer the question. ListView also supports source collections that implement ISelectionInfo, which is a way for the source to have full control over the selection, but it's pretty advanced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kaiguo @MikeHillberg Yeah, I'd say selecting a group of items should be as simple as possible. Right now, looking at the TreeView Selection API, I need to interact with the specific
TreeView
object to select multiple items. Hence, if I just wanted to mark a group of items in a TreeView as selected, I can't easily do that without somehow referencing an object of the View (as in MVVM). That seems to me like a pretty big oversight and an unnecessary hurdle for the developer.