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.

Access the selected items in a listview when a Button clicks.

SreeeeSreeee INMember ✭✭✭✭✭

I have a switch in my listview and I select some of the items like below screenshot.

When I click the Add button how can I access the selected items?

Best Answer

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited May 2018 Accepted Answer

    Hi @seanyda and @AlessandroCaliaro
    Got the id of the selected item by below code:

    In the OnToggledEvent(object sender, EventArgs args) we can get the Parent ViewCell depending on your hierarchy:

    public void OnToggledEvent(object sender, EventArgs args)
    {
        //Here I use a Grid to wrap the content so I need to use two Parent to find the ViewCell
        ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
    
        // If you set the list<UserProfileHBList> as the ListView's ItemsSource, we can find the model through BindingContext
        UserProfileHBList model = cell.BindingContext as UserProfileHBList;
    
        // Then the userId can be known
        if (model != null)
        {
            Debug.WriteLine("Userid:>>"+model.userProfileTO.userId);
        }
    
    }
    

Answers

  • seanydaseanyda GBMember ✭✭✭✭✭

    I'd use System.Linq and filter the list...

    var toggledUsers = listName.Where(w=>w.SwitchBool == true).ToList();

  • SreeeeSreeee INMember ✭✭✭✭✭

    @seanyda said:
    I'd use System.Linq and filter the list...

    var toggledUsers = listName.Where(w=>w.SwitchBool == true).ToList();

    Tried this but getting an error: "listview does not contain a definition for where"

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    You have to filter the ObservableCollection, not the listview

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Sreeee said:

    @seanyda said:
    I'd use System.Linq and filter the list...

    var toggledUsers = listName.Where(w=>w.SwitchBool == true).ToList();

    Tried this but getting an error: "listview does not contain a definition for where"

    You need to add using System.Linq as a reference at the top. Also it's not an extension for ListView, it's an extension on IEnumerable which is any list. You need to add the Where() statement on the end of your ListViews ItemSource list.

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited May 2018

    Hi @seanyda and @AlessandroCaliaro Is it possible to get all selected users using Toggled="OnToggledEvent"

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Sreeee said:
    Hi @seanyda and @AlessandroCaliaro Is it possible to get all selected users using Toggled="OnToggledEvent"

    There are many different approaches you can take. Using the OnToggledEvent would allow you to add/remove items to a list as it's toggled. But it really depends what approach you want to take. System.Linq like I described earlier would be the quickest way.. but it depends how you've wrote the code.

  • SreeeeSreeee INMember ✭✭✭✭✭

    @AlessandroCaliaro and @seanyda I started working with OnToggledEvent and trying to get the id of the toggled item.
    I try like below but getting an exception:

    public class MyToggledEventArgs : EventArgs
            {
                public UserProfileHBList MyItem { get; set; }
                public MyToggledEventArgs(UserProfileHBList item)
                {
                    this.MyItem = item;
                }
            }
    
            void OnToggledEvent(object sender, MyToggledEventArgs args)
            {
                var item = args.MyItem;
                if (item != null)
                {
                    Debug.WriteLine("Userid:>>"+item.userProfileTO.userId);
                }
            }
    
       //Model class
    public class DirectoryResponse
        {
            public List<UserProfileHBList> userProfileHBList { get; set; }
        }
    
        public class UserProfileHBList
        {
            public UserProfileTO userProfileTO { get; set; }
        }
    
        public class UserProfileTO
        {
            public string userId { get; set; }
            public string firstName { get; set; }
            public string email { get; set; }
            public string lastName { get; set; }
        }
    

    Is there any other way to get the userid?

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited May 2018 Accepted Answer

    Hi @seanyda and @AlessandroCaliaro
    Got the id of the selected item by below code:

    In the OnToggledEvent(object sender, EventArgs args) we can get the Parent ViewCell depending on your hierarchy:

    public void OnToggledEvent(object sender, EventArgs args)
    {
        //Here I use a Grid to wrap the content so I need to use two Parent to find the ViewCell
        ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
    
        // If you set the list<UserProfileHBList> as the ListView's ItemsSource, we can find the model through BindingContext
        UserProfileHBList model = cell.BindingContext as UserProfileHBList;
    
        // Then the userId can be known
        if (model != null)
        {
            Debug.WriteLine("Userid:>>"+model.userProfileTO.userId);
        }
    
    }
    
Sign In or Register to comment.