2

how to select item in listview Mvvm my code did't working

my code Itemsource:

protected async override void OnAppearing()
    {
        base.OnAppearing();
        var allPersons = await firebaseHelper.GetAllCamps();
        lstCampus.ItemsSource = allPersons;
    }

my code selectedItem:

void OnItemTapped(object sender, System.EventArgs e)
    {
        if (lstCampus.SelectedItem != null)
            DisplayAlert("OnItemTapped", lstCampus.SelectedItem.ToString(), "OK");
    }

my model :

public class Campus
{
    public string NameCamp { get; set; }
}

my view model:

public async Task<List<Campus>> GetAllCamps()
    {

        return (await firebase
          .Child("Camps")
          .OnceAsync<Campus>()).Select(item => new Campus
          {
              NameCamp = item.Object.NameCamp
          }).ToList();
    }

Thank u

0

1 Answer 1

3

You can try to use behaviours. This will help you:

https://www.c-sharpcorner.com/article/xamarin-forms-eventtocommand-behavior-in-mvvm-viewmodel/

https://learn.microsoft.com/en-US/xamarin/xamarin-forms/app-fundamentals/behaviors/reusable/event-to-command-behavior

After that just add something like this to your list:

                <ListView.Behaviors>
                    <b:EventToCommandBehavior EventName="ItemTapped" Command="{Binding TappedCamp}" InputConverter="{StaticResource ItemTappedConverter}"/>
                </ListView.Behaviors>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.