Set multiple selected item in ListView from CodeBeging

BullT 41 Reputation points
2022-01-19T17:37:20.52+00:00

Hi,

How can I select multiple items in ListView from Codebegin in C# UWP.

To get selected items I do this:

            foreach (User userSelected in UsersListView.SelectedItems)
            {
                Users.Add(userSelected);
            }

But if I do that for set, in the listview only the last one is selected:

                foreach (User userSelected in Users)
                {
                    UsersListView.SelectedItem = userSelected);
                }

This is the xaml:

<ListView x:Name="UsersListView" ItemsSource="{x:Bind Users}" SelectionMode="Multiple">

How can I select multiple items?

Thanks,
BullT

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,003 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2022-01-20T02:48:41.227+00:00

    Hello,
    Welcome to Microsoft Q&A!

    But if I do that for set, in the listview only the last one is selected:

    It's by default, the SelectedItem was used to get or set SelectedItem. it is single item but not collection . if you want to Set multiple selected item in ListView from code behind. You could call UsersListView.SelectedItems.Add method to add the selected item.

    foreach (User userSelected in Users)  
    {  
        UsersListView.SelectedItems.Add(userSelected);  
    }  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.