In WinUI 3 ListView how to clear selection using MVVM

Boris Kleynbok 1 Reputation point
2022-08-18T16:40:23.107+00:00
 <ListView x:Name="UserRoles" ItemsSource="{x:Bind ViewModel.UserRoles, Mode=OneWay}"  
                                      SelectionMode="Multiple"  
                                      VerticalContentAlignment="Top"    
                                      ScrollViewer.IsVerticalRailEnabled="True"  
                                      ScrollViewer.VerticalScrollBarVisibility="Visible">  
                                <i:Interaction.Behaviors>  
                                    <ic:EventTriggerBehavior EventName="SelectionChanged">  
                                            <ic:InvokeCommandAction Command="{x:Bind ViewModel.SaveSelectedItemsCommand}" CommandParameter="{x:Bind UserRoles.SelectedItems,Mode=OneWay}"/>  
                                    </ic:EventTriggerBehavior>  
                                </i:Interaction.Behaviors>  
                                </ListView>  
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
723 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 14,446 Reputation points Microsoft Vendor
    2022-08-19T10:03:42.087+00:00

    Hello @Boris Kleynbok
    Welcome to Microsoft Q&A!

    I see that your SaveSelectedItemsCommand is passed the parameter UserRoles.SelectedItems.
    So, just need to create a new variable in the ViewModel to point to SelectedItems and execute Clear() when the selection needs to be cleared.

    The following code implements the selection three times, then clears all selections.

    XMAL:

    <ic:EventTriggerBehavior EventName="SelectionChanged">  
        <ic:InvokeCommandAction  
                                Command="{x:Bind MainViewModel.ClearCommand,Mode=OneWay}"  
                                CommandParameter="{x:Bind UserRoles.SelectedItems,Mode=OneWay}"/>  
    </ic:EventTriggerBehavior>  
    

    ViewModel:

    private RelayCommand<object> _clearCommand;  
            public RelayCommand<object> ClearCommand  
            {  
                get  
                {  
                    _clearCommand = new RelayCommand<object>((s) =>  
                    {  
      
                       var temp = s as IList<object>;  
                        if (temp.Count >= 3)  
                        {  
                            temp.Clear();  
                        }  
      
                   });  
      
                   return _clearCommand;  
                }  
                set  
                {  
                    _clearCommand = value;  
                }  
            }  
    

    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 comments No comments