locked
Selecting an already Highlighted item in a ListView does not call the SelectedItem method. RRS feed

  • Question

  • User341975 posted

    When I click on an Item in a ListView, it calls the SelectedItem method and the code takes me to the details page. However, when I click on the back button to come back to the Main Page and click on exactly the same item nothing happens. If I click on another item on the list, it calls the method but not the current one. Is this a bug in ListView or is it meant to work this way? Here's the Xaml xaml <ListView x:Name="CarListView" ItemsSource="{Binding Items}" CachingStrategy="RecycleElement" SelectedItem="{Binding SelectedCar}" RowHeight="60"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Margin="8"> <Label Text="{Binding Make}" FontAttributes="Bold" /> <Label Text="{Binding YearOfModel}" /> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>

    Wednesday, March 20, 2019 11:02 PM

Answers

  • User2148 posted

    You can use ItemTapped

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, March 21, 2019 5:48 AM

All replies

  • User2148 posted

    You can use ItemTapped

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, March 21, 2019 5:48 AM
  • User350 posted

    @AlessandroCaliaro said: You can use ItemTapped

    Indeed. The behaviour you are seeing is the design of ListView. The item is still selected so tapping it again will not fire the SelectedItem method.

    Thursday, March 21, 2019 6:46 AM
  • User369979 posted

    @toyins If you don't want this default behavior, you could uncheck the list view's selection when the user clicks the back button to come back:

    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        var viewModel = BindingContext as MainPageViewModel;
        viewModel.SelectedCar = null;
    }
    
    Thursday, March 21, 2019 8:09 AM
  • User350 posted

    @LandLu said: @toyins If you don't want this default behavior, you could uncheck the list view's selection when the user clicks the back button to come back:

    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        var viewModel = BindingContext as MainPageViewModel;
        viewModel.SelectedCar = null;
    }
    

    Yes, this is an approach we've used. Ideally, if your MVVM framework exposes Appearing events in the ViewModel (which FreshMvvm and MvvmCross do), you can/should do this in the ViewModel rather than in xaml.cs code behind.

    Another consideration is whether the user experience is good if they tap to go to the item details, then when they go back the item is no longer selected. This could be considered a bad UX feature.

    Thursday, March 21, 2019 9:42 AM
  • User341975 posted

    @LandLu, @JamesLavery The onAppearing does not work. The ItemTapped suggested by @AlessandroCaliaro seems to work but the SelectedCar in the ViewModel is called twice by the SelectedItem Method and the ItemTapped Event.

    I guess, I have to use the ItemTapped Event instead of the SelectedItem method.

    Thursday, March 21, 2019 10:16 AM
  • User369979 posted

    @toyins You could add an if statement, do the push operation when the value != null.

    Thursday, March 21, 2019 10:19 AM
  • User341975 posted

    @LandLu, where do I add the if statement and how? Please expatiate.

    Thursday, March 21, 2019 11:10 AM
  • User369979 posted

    @toyins If you want to navigate to the detail page in the SelectedCar's set method with MVVM, you could try:

    Model _selectedCar;
    public Model SelectedCar
    {
        set
        {
            _selectedCar = value;
            OnPropertyChanged();
            if (value != null)
            {
                // Do the push
            }
        }
        get
        {
            return _selectedCar;
        }
    }
    

    Or you could use ItemTapped event instead.

    Thursday, March 21, 2019 11:28 AM
  • User341975 posted

    @LandLu oh, that's how it's currently set up.

    Thursday, March 21, 2019 12:34 PM
  • User240947 posted

    Hi @toyins, try this code

    private async void ListViewItemSelected(object sender, SelectedItemChangedEventArgs e) {

                if ((sender != null && ListView.SelectedItem != null))
                {
    
                }
                educationListView.SelectedItem = null;
        }
    
    Friday, March 22, 2019 12:28 PM
  • User305593 posted

    You are totally wrong omg. You made waste time. Totally. I just had to create a GestureRecognizer like this

    Later, in my viewmodel i received the command always i pushed my main frame. Consider this solution if you need and answer for this problem. Grettings.

    Edit: I dont know why the code doesn't show. But the solucion is you use a TapGestureRecognizer inside a GestureRecognizers

    Wednesday, June 19, 2019 11:13 PM