Skip to content
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

Not possible to select treeview nodes programmatically #386

Closed
knightmeister opened this issue Mar 6, 2019 · 14 comments · Fixed by #243
Closed

Not possible to select treeview nodes programmatically #386

knightmeister opened this issue Mar 6, 2019 · 14 comments · Fixed by #243
Assignees
Labels
area-TreeView bug Something isn't working team-Controls Issue for the Controls team

Comments

@knightmeister
Copy link

Describe the bug
Previous Windows tree views had properties such as SelectedNodes which allowed you to set the nodes which are selected/highlighted in the control.

The UWP treeview exposes a SelectedNodes property which is a collection. The collection is read only and calling .Add on it and specifying a TreeViewNode does not add it to the collection and does not select the item in the user interface.

An IsSelected property should also be added to TreeViewNode to match with TreeViewItem.

Steps to reproduce the bug
Add items to a TreeView and then use the following code, which you would expect would select the first item.

if (treeView.RootNodes.Count > 0)
                treeView.SelectedNodes.Add(treeView.RootNodes[0]);

SelectedNodes = 0

Expected behavior
The first tree node would be selected.

Version Info
1809

@jevansaks
Copy link
Member

@kaiguo Is this something you fixed recently or is it part of the pending PR you have out?

@kaiguo
Copy link
Contributor

kaiguo commented Mar 6, 2019

SelectedNoes collection only works in multi-select mode, @knightmeister can you set TreeView to multi-select and see if it still repros?

We are working on adding a new SelectedNode property for single selection, see PR #243.

@jevansaks
Copy link
Member

Shouldn't we make SelectedNodes contain the one and only selected item in single select? I'm pretty sure Selector.SelectedItems works this way.

@kaiguo
Copy link
Contributor

kaiguo commented Mar 6, 2019

@jevansaks Yeah I think that works, then we can probably just get rid of the singular form SelectedItem and SelectedNode?

@knightmeister
Copy link
Author

@kaiguo Yes, it works when SelectionMode=Multiple.

@jevansaks
Copy link
Member

@jevansaks Jevan Saks FTE Yeah I think that works, then we can probably just get rid of the singular form SelectedItem and SelectedNode?

No, having the singular one is still convenient when in single select mode.

@kaiguo
Copy link
Contributor

kaiguo commented Mar 6, 2019

I see. I'll make some updates in #243

@jevansaks
Copy link
Member

Ok, i'll assign this to you then.

@jevansaks jevansaks added bug Something isn't working area-TreeView labels Mar 7, 2019
kaiguo pushed a commit that referenced this issue Jun 5, 2019
@dpaulino
Copy link
Contributor

dpaulino commented Jul 4, 2019

Just wanted to chime in and ask if there's any progress on this? I really need to be able to programmatically set the selected item

@kaiguo
Copy link
Contributor

kaiguo commented Jul 9, 2019

Just wanted to chime in and ask if there's any progress on this? I really need to be able to programmatically set the selected item

Hey @dpaulino, sorry I've been working on rounded corners stuff. Will get back to this some time next week hopefully.

kaiguo pushed a commit that referenced this issue Jul 30, 2019
* Add IsContentMode on TreeViewList

* Add SelectedNode

* Add TreeViewSelectedNodeTest

* Add SelectedItem

* Refactor

* Add SelectedItem test

* Fix tests

* Add SelectedItems

* Update ViewModel

* Update tests

* Remove unused code

* Add ItemFromNode

* Add comments

* Skip tests on lower os versions

* Update null checks

* Update tests

* Cleanups

* Update ItemFromNode

* Ignore m_originalNode when updating selection

* Update NodeFromItem

* Update ViewModel reference to be weak ref

* Add GetViewModel

* Update weak refs

* Set the new apis to preview

* Fix #386

* Fix NodeFromItem

* Fix test

* Cleanups

* Fix single selection

* Cleanups

* More cleanups
@msft-github-bot
Copy link
Collaborator

🎉This issue was addressed in #243, which has now been successfully released as Microsoft.UI.Xaml v2.2.190731001-prerelease.:tada:

Handy links:

@dpaulino
Copy link
Contributor

@kaiguo hey, just wanted to triple check. With your PR regarding this, should I now be use TwoWay binding for SelectedItem. Like this?

<u:WorkspaceTreeView 
    x:Name="Tree" 
    ItemsSource="{x:Bind ViewModel.WorkspaceItems, Mode=OneWay}"
    SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay}"

@kaiguo
Copy link
Contributor

kaiguo commented Oct 10, 2019

@kaiguo Kai Guo FTE hey, just wanted to triple check. With your PR regarding this, should I now be use TwoWay binding for SelectedItem.

Yes, you can use binding like that for single selection.
SelectedItems for multi-select is still read only (we kept it consistent with ListView), so you'll have to do it through a TreeView instance, like the example we showed in this spec.

@dpaulino
Copy link
Contributor

Okay thanks for confirming. I think I'm getting an access violation error when I use two way binding though... But I'll try to get a proper repro, and I'll create a new issue if I see any issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-TreeView bug Something isn't working team-Controls Issue for the Controls team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants