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.