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.

Xamarin forms switch list event double-firing

ossentooossentoo GBMember ✭✭

Hi,

I have a list of countries in a listview grouped by region (Europe, Americas, Asia Pacific etc...). There is another switch on each region that allows me to select/deselect all countries in a region. There's a binding on the region level switch which runs code the code in the view model. This works well.

I've also got some code in the viewmodel which runs every time a country level switch is selected. The purpose for this is to check if all countries are selected, and one country is deselect, unselect the region level switch.

The impact of this is that this causes the region level selection to fire unexpectedly.

So, in this image, if I deselect Algeria, the Middle East & Africa switch is turned off (which is expected). This then fires the binding call back on the Selected property for that switch, which causes all the countries to the deselected.

The question is, is there is way in code to temporarily stop the binding calling back of switch? The xaml example of each switch in the listview is:


The solution is build with Xamarin Forms PCL.

Best Answer

  • ossentooossentoo GBMember ✭✭
    Accepted Answer

    Ended up using a custom Switch control, and overriding Propertychanged by using a load of if statements and boolean fields.

    The boolean fields differentiated between:

    • a change that happened just to user interaction on the screen with the parent switch
    • a change that happened as a result of a child switch (which changed the parent in code).

Answers

  • ossentooossentoo GBMember ✭✭
    Accepted Answer

    Ended up using a custom Switch control, and overriding Propertychanged by using a load of if statements and boolean fields.

    The boolean fields differentiated between:

    • a change that happened just to user interaction on the screen with the parent switch
    • a change that happened as a result of a child switch (which changed the parent in code).
Sign In or Register to comment.