In MAUI, when I clear the items source of a list view, the selected Item is still set.

ComptonAlvaro 166 Reputation points
2022-09-02T10:08:21.2+00:00

I have a listview which item source is binded to a observable collection of a view model. This view model has a observable collection that is the item source of the listview. This view model has another property for the selected item.

When I select a an item in the list view, the selected item property of the view model is set too. That's ok, but when I clear the collection and add new items to refresh the collection, the selected item is still set.

In WPF the behavior is to set to null the selected item when I clear the collection.

The code is this:

My listview in the view:

   <ListView x:Name="lvAplicaciones" Background="red"  
               Margin="0, 25"  
               MaximumHeightRequest="200"  
               VerticalScrollBarVisibility="Always"  
               ItemsSource="{Binding Aplicaciones}"  
               SelectedItem="{Binding AplicacionesSelectedItem, Mode=OneWayToSource}"  
               IsPullToRefreshEnabled="True"  
               RefreshCommand="{Binding RefrescarAplicacionesCommand}"  
               IsRefreshing="{Binding EsRefrescarOcupado, Mode=OneWay}"  
               RefreshControlColor="Orange">  
     
       <ListView.Header>  
           <StackLayout Orientation="Horizontal" Background="orange">  
               <Label Text="Aplicaciones"  
                       HorizontalTextAlignment="Start"  
                       TextColor="Olive"></Label>  
           </StackLayout>  
       </ListView.Header>  
         
       <ListView.ItemTemplate>  
           <DataTemplate>  
               <TextCell Text="{Binding Nombre}"/>  
           </DataTemplate>  
       </ListView.ItemTemplate>  
   </ListView>  

The view model:

   public ObservableCollection<Aplicacion> Aplicaciones { get; } = new ObservableCollection<Aplicacion>();  
     
   private Aplicacion _aplicacionesSelectedItem;  
     
   public Aplicacion AplicacionesSelectedItem  
   {  
       get { return _aplicacionesSelectedItem; }  
       set  
       {  
           SetAplicacionesSelectedItem(value);  
       }  
   }  
     
   private void SetAplicacionesSelectedItem(Aplicacion paramAplicacionSeleccionada)  
   {  
       if (_aplicacionesSelectedItem != paramAplicacionSeleccionada)  
       {  
           _aplicacionesSelectedItem = paramAplicacionSeleccionada;  
           RefrescarAcciones(_aplicacionesSelectedItem?.Acciones);  
           base.RaisePropertyChangedEvent(nameof(AplicacionesSelectedItem));  
       }  
   }  
     
     
     
   private List<Aplicacion> GetAplicaciones()  
   {  
       List<Aplicacion> miLstAplicaciones = new List<Aplicacion>();  
       for (int i = 0; i < 3; i++)  
       {  
           List<Accion> miLstAcciones = new List<Accion>();  
           for (int j = 0; j < 2; j++)  
           {  
               Accion miAccion = new Accion("Accion0" + j, "Descripcion0" + j);  
               miLstAcciones.Add(miAccion);  
           }  
     
           Aplicacion miAplicacion = new Aplicacion("Aplicacion0" + i, miLstAcciones);  
           miLstAplicaciones.Add(miAplicacion);  
       }  
     
     
     
       return miLstAplicaciones;  
   }  
     
     
   private void GetAplicacionesYRefrescarAsync()  
   {  
       RefrescarAplicaciones(GetAplicaciones());  
   }  
     
     
   private void RefrescarAplicaciones(IEnumerable<Aplicacion> paramIeAplicaciones)  
   {  
       Aplicaciones.Clear();  
       if(paramIeAplicaciones?.Count() > 0)  
       {  
           foreach(Aplicacion iteradorAplicacion in paramIeAplicaciones.OrderBy(x => x.Nombre))  
           {  
               Aplicaciones.Add(iteradorAplicacion);  
           }  
       }  
   }  
     
     
     
   public ICommand RefrescarAplicacionesCommand { get; private set; }  
     
   private async Task OnRefrescarAplicacionesCommand()  
   {  
       try  
       {  
           EsRefrescarOcupado = true;  
     
           GetAplicacionesYRefrescarAsync();  
       }  
       catch (Exception ex)  
       {  
           await Application.Current.MainPage.DisplayAlert("ERROR", ex.Message, "OK");  
       }  
       finally  
       {  
           EsRefrescarOcupado = false;  
       }  
   }  

When I set a breakpoint at the end of the method OnRefrescarAplicacionesCommand, the property AplicacionesSelectedItem is still set to the item that it was slected, it is not null.

Is this the expected behavior?

Thanks.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,822 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 34,761 Reputation points Microsoft Vendor
    2022-09-05T06:07:04.777+00:00

    Hello,

    When I set a breakpoint at the end of the method OnRefrescarAplicacionesCommand, the property AplicacionesSelectedItem is still set to the item that it was slected, it is not null.
    Is this the expected behavior?

    Yes, the Clear method could remove the items from it, not dispose them.

    Therefore, after calling Clear, the SelectedItem is no null.

    Best Regards,

    Alec Liu.


    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