Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

List View : How to find selected item when there are duplicates

Hello,

I'm new to Xamarin and mobile app development.
I've always used lstItems.SelectedIndex to find the selected item when there are duplicate items in a list. But with Xamarin.forms, this doesn't seem to be an option. I am very new (as of this morning..) to using view models and binding. I think I get the basic idea, but I'm very stuck and need some guidance.

Lets assume the list is named lstItems. There may be multiple of the same items on the list. How can I tell which item has been selected?

Thank you in advance.

Best Answer

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    You can create a public static ObservableCollection<Person> selectItems { get; set; } to contains selected item in ViewModel. Then bind it to the listview's SelectedItem="{Binding selectItems}". But I do not know how did you acheve the muti-select for listview. I use click item event to judge it like following GIF.

    Here is layout listview.

          <ListView x:Name="mylistview" 
                      ItemsSource="{Binding persons}"   
                      CachingStrategy="RecycleElement" 
                      SeparatorVisibility="None" 
                      HasUnevenRows="True" 
                      SelectedItem="{Binding selectItems}"
                      ItemSelected="mylistview_ItemSelected"        
                      >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell >
                            <ViewCell.View>
                    <Frame IsClippedToBounds="False" 
                                                   Margin="0,5" 
                                                   CornerRadius="10" 
                                                   Padding="0" 
                                                   HasShadow="False" 
                                                   BorderColor="#f0f0f0" 
                                                   BackgroundColor="Transparent" >
    
                                    <StackLayout Padding="0" Spacing="0" MinimumHeightRequest="0">
                                        <StackLayout Orientation="Horizontal">
                                            <Label Text="{Binding Name}" Margin="13,12,5,0" FontAttributes="Bold" FontSize="18" TextColor="#222222">
    
                                            </Label>
                                            <Frame MinimumHeightRequest="0" VerticalOptions="EndAndExpand" Padding="7,2,7,2" CornerRadius="100" BackgroundColor="Blue" HasShadow="False">
    
                                                <Label Text="{Binding Image}" TextColor="White" FontSize="Micro"/>
                                            </Frame>
                                        </StackLayout>
                                        <Label Text="{Binding Count}" Margin="13,6,0,12">
                                        </Label>
                                    </StackLayout>
                                </Frame>
                            </ViewCell.View>
                        </ViewCell>  
                    </DataTemplate>
    
                </ListView.ItemTemplate>
              </ListView>
    

    Here is layout background code.

     public partial class MainPage : ContentPage
        {
            PersonsViewModel personsViewModel;
    
    
            public MainPage()
            {
               InitializeComponent();
    
                personsViewModel =  new PersonsViewModel(Navigation,this);
                this.BindingContext = personsViewModel;
    
    
    
            }
    
            private async void mylistview_ItemSelected(object sender, SelectedItemChangedEventArgs e)
            {
                var person = e.SelectedItem as Person;
    
    
                if (PersonsViewModel.selectItems.Contains<Person>(person))
                {
                    await DisplayAlert("waring", "select the duplicated item", "Ok");
                }
                else
                {
    
                    PersonsViewModel.selectItems.Add(person);
                }
            }
    }
    

    Here is PersonsViewModel.cs

     public class PersonsViewModel
        {
    
    
            public ObservableCollection<Person> persons { get; set; }
    
            public static ObservableCollection<Person> selectItems { get; set; }
    
    
    
    
            public PersonsViewModel(INavigation navigation, ContentPage page)
            {
                selectItems= new ObservableCollection<Person>();
    
                persons = new ObservableCollection<Person>();
                persons.Add(new Person() { Name=  "Leon1" ,Image= "https://aka.ms/campus.jpg", Count=1 , Isfavourite=false});
                persons.Add(new Person() { Name = "Leon2", Image = "Clover.png", Count = 2 , Isfavourite = false });
                persons.Add(new Person() { Name = "Leon3", Image = "Clover.png", Count = 3 , Isfavourite = false });
                persons.Add(new Person() { Name = "Leon4", Image = "Clover.png", Count = 4 , Isfavourite = false });
                persons.Add(new Person() { Name = "Leon5", Image = "Clover.png", Count = 5, Isfavourite = false });
                persons.Add(new Person() { Name = "Leon6", Image = "Clover.png", Count = 6 , Isfavourite = false });
                persons.Add(new Person() { Name = "Leon7", Image = "Clover.png", Count = 7, Isfavourite = false });
                persons.Add(new Person() { Name = "Leon8", Image = "Clover.png", Count = 8 , Isfavourite = false });
                persons.Add(new Person() { Name = "Leon9", Image = "Clover.png", Count = 9 , Isfavourite = false });
                persons.Add(new Person() { Name = "Leon10", Image = "Clover.png", Count = 10 , Isfavourite = false });
    
    
               }
    }
    
    
  • ScottVentsUSScottVentsUS Member ✭✭

    Lets say there are multiple "Leon" in your list, how can you tell which one the user selected?

Sign In or Register to comment.