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?
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
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 have to filter the ObservableCollection, not the listview
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.
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.
@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:
Is there any other way to get the userid?
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: