I have a listview, which shows 20 users initially. Whenever the listview bottom reaches a new REST API call will start and it shows more users(20,40,60 etc). When loading more users the list gets refreshed.
Listview items have a switch option and if I press the switch option the userid of that user is added to a list. If I again press the same switch that userid is removed from the list.
My problem is the selected user's switches are going to off state when loading more users. But the userids saved in the list have no problem, the list contains the selected user's id. So how can I on the switch of already selected users after loading more users?
Thanks in advance
Yes this is correct to bind your switch's IsToggled
to point out which user has been selected.
Also you can define an extra property in your model. Then you can change this property directly in your OnToggledEvent
instead of using a converter.
I created a new property like below and assign that property directly to OnToggledEvent.
public bool isToggledUser { get { bool toggle = false; string selectedIds = Application.Current.Properties["GroupUserIds"].ToString(); if (!string.IsNullOrWhiteSpace(selectedIds)) { List<int> TagIds = selectedIds.Split(',').Select(int.Parse).ToList(); if (TagIds.Contains(Int32.Parse(userId))) { toggle = true; } else { toggle = false; } } else { toggle = false; } return toggle; } }
But the problem is the switch already have toggled event Toggled="OnToggledEvent". When loading more items also it fires.
Now I need to stop the OnToggledEvent function codes when loading more items. Is that possible?
Answers
Please show your code to help us figure out what's your issue.
@LandLu I am working on a feature like WhatsApp group. I can add members to the groups. When I am trying to add members to a group, a list of users UI is shown with a switch option on the right-hand side of each user. Screenshot adding below.

When clicking on a switch I will save the selected user's id to a list using the Toggled property.
Xaml code:
On the OnToggledEvent() I will save the user id to a list.
The listview shows 20 users initially. Whenever the listview bottom reaches a new REST API call will start and it shows more users(20,40,60 etc). When loading more users the list gets refreshed and the already selected switches get disabled. But the already selected user's ids are saved in a list.
@LandLu How I tried to solve this problem
When loading more items I will convert the list to a comma separated string and save that comma separated string to local DB like below. Where userIds is the list.
Using IsToggled property I bind the current userid and create a converter to check that userid is already selected or not.
In the converter, change the comma-separated string again to a list and check the current userid is exist or not in that list. If exist return true value and if not exist return false value.
After doing the above codes also no changes in the current issue. Is this approach correct?
Thanks in advance
Yes this is correct to bind your switch's
IsToggled
to point out which user has been selected.Also you can define an extra property in your model. Then you can change this property directly in your
OnToggledEvent
instead of using a converter.Just a quess but does your ConvertBack Method get called when adding the next 20 items?
Some items dont have oneway binding as the default. Maybe the switch is one of those.
Maybe it has two way binding which throws then the convertback method.
Try setting it to oneway.
Hi @Gigex42 and @LandLu
I created a new property like below and assign that property directly to OnToggledEvent.
But the problem is the switch already have toggled event Toggled="OnToggledEvent". When loading more items also it fires.
Now I need to stop the OnToggledEvent function codes when loading more items. Is that possible?
@Gigex42 Convert back method giving NotImplementedException. If I remove the throw new NotImplementedException(); in my ConvertBack method, everything will work fine. Or explicitly set the binding mode to be One-way.
Hi @LandLu,@Gigex42 and @Charwaka Still I have an issues with this. Can you guys help me out?
I need to stop the OnToggledEvent function codes when loading more items. Is there any way to stop it? I will show my codes.
Switch code:
OnToggledEvent(): When I click a switch this fuction will fire and I will take the userid of the item and save it into a list. If I again press the same item that userid will be removed from the list. Evreytime when button trigger(on or off) this function will execute. But When loading more items I need to stop the execution of this code. Because when loading more items there is a chance of toggling of switch if we already select switch, as a result that userid will be removed from the list. I need to stop that.
Loading more items code:
The LoadMore(e) will call the REST API for loading more items. At this time I need to stop the OnToggledEvent() codes. But don't know how to stop it. Please help me, thanks in advance
Hi @LandLu,@Gigex42 and @Charwaka I tried the following logic:
I declared a bool variable and set false value initially for that.
When loading more items I changed the bool variable value as true. After loading completed I changed the value to false.
Also inside of OnToggledEvent I am checking the value of this bool variable.
But the problem is, the bool value returns false value when loading more items and entering inside of the OnToggledEvent() codes. As a result already saved userids from the list are removing. Is this approach correct or any other way to solve this?
Did
LoadMore(e);
method consume the loading code? It seems this method isn't a task one, and the code after it will not wait until your request has been completed.Try to modify it like:
And use it like:
Hi @LandLu : I am adding my LoadMore(e) codes and its methods.
My LoadMore(): Where DVM is the viewmodel object.
Viewmodel Codes:
Binding AllItems to listview
@LandLu Can you suggest where I can place the await property. I tried like below:
Added the loadmore value changing code after the acr userdialog code. But this also gives the wrong result. After loading more items the bool value returns true value, not changing to false.
I notice you use load more command to load the data from server. Try to separate this method. I think this will help:
Then
LoadMore()
is a task method. You can use the code we talked above:After doing this also same result. When loading more items the value of bool is false.
@Sreeee Need your sample to help me understand your issues.
@Sreeee Why dont you create a bool property in model and bind value ? sorry if i understood your question wrong
Can you send a sample ZIP Code.
@Charwaka and @LandLu
I will provide the sample zip code ASAP.
@Charwaka I am using the bool value to know the loading more items scenario, at that time the code execution not go inside of the OnToggledEvent(). But how I can manage it with a bool model property?
yes put a bool value in ObservableList Collection
public class student
{
public string Name;
Public bool isValidStudetn // use this to bind Toggle
}
ObservableCollection collection = new ObservableCollection();
//Add Some Data
Listview.ItemSource=collection; Or Bind through Front End (Mode=TwoWay)
@Charwaka and @LandLu
I created a sample project, but having some issues with that. The UI is blank, binding of REST data to listview is not working. I will fix it and provide the sample project ASAP.
@Sreeee Change your loading data logic to
Fire the
LoadMoreItems
method when the listView has reached the last item instead of place your logic code to this method. SinceItemAppearing
will always be fired if the listView has been scrolled causing an item's disappearing. Then yourUtility.loadMore = false;
will be run again and again so it seems this property is always false.And my code above will make sure
LoadMoreItems(e);
only fires when the list view has been scrolled to the last item. TheloadMore
will be true if you make a break point to check this.@LandLu Still getting false value for loadmore when loading more items. True value is not assigning into it when loading more items.
Following is my latest code, can you please check.
@Sreeee This will make it work if you add a break point in
OnToggledEvent
. But I'm confused about what you want. I've seen you have added an alert window when loading data, this will intercept user clicking the switch. And You have refreshed your allItems list every time getting data form your server. What is youruserIds
used for?I think your code logic is too complicated. When you change the switch's state, post this action to your server. Change your original data on the server to achieve what you want.