We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

SfCheckbox fires StateChanged whenever I scroll the listview

Hello! I have a SfCheckbox embedded inside a SfListview and I use the following to catch the change of Checkbox via Viewmodel using xaml code as follows:


<button:SfCheckBox.Behaviors>

                                                            <local1:EventToCommandBehavior

                                                                Command="{Binding Path=BindingContext.CheckBox_StateChangedCommand, Source={x:Reference Name=CartPage}}"

                                                                CommandParameter="{Binding .}"

                                                                EventName="StateChanged" />

                                                        </button:SfCheckBox.Behaviors>


in viewmodel:

        private async Task CheckBox_StateChanged(object obj)

        {

            var item = obj as CartModel;


            await App.Current.MainPage.DisplayAlert(item.rowcheck.ToString(), listview.IndexOf(item) + item.name, "OK");

        }


But whenever I scroll the listview and if the item inside the listview is hidden and visible again, the event is fired. Because TapGestureRecognizer can't work with checkbox. How can I do this in the most correct and correct way? Thank you.


6 Replies

SR Shivani Ramakrishnan Syncfusion Team March 17, 2023 11:39 AM UTC

Hi Hoang,


Query: SfCheckbox fires StateChanged whenever I scroll the listview


We have checked your query and prepared the sample based on the requirements using EventToCommand behaviour. We have attached the sample for your reference and kindly refer the sample and please let us know if you need any other details.


Regards,

Shivani


Attachment: CheckBoxSample1_c9dabe72.zip


HG hoang Gia replied to Shivani Ramakrishnan March 17, 2023 03:21 PM UTC

Thank you for your response.


I'm using listview with grouped and loadmore, the Sfcheckbox is binding to a Property from the Item ViewModel, so when I scroll the Listview the event fired and the item seem display randomly, its hard to get the correct index of the item too, :(

<button:SfCheckBox

                                                        x:Name="checkBox"

                                                        Grid.Column="0"

                                                        CheckedColor="Green"

                                                        IsChecked="{Binding rowcheck}"

                                                        IsThreeState="False">

                                                        <button:SfCheckBox.Behaviors>

                                                            <local1:EventToCommandBehavior

                                                                Command="{Binding Path=BindingContext.CheckBox_StateChangedCommand, Source={x:Reference Name=CartPage}}"

                                                                CommandParameter="{Binding .}"

                                                                EventName="StateChanged" />

                                                        </button:SfCheckBox.Behaviors>

                                                    </button:SfCheckBox>

Is there anyway to do this but just do binding context just once? (I did it once only in the code behind, not viewmodel)



Thanks,



CP Chozarajan Pandiyarajan Syncfusion Team March 21, 2023 12:30 AM UTC

Hi Hoang,

 

Currently, we are validating the reported query with high priority and we will let you know the details on March 23rd, 2023. We appreciate your patience until then

 

Thanks,



AK Arunachalam Kandasamy Raja Syncfusion Team March 23, 2023 12:35 PM UTC

Hi Hoang, 

 

Query: SfCheckbox fires StateChanged whenever I scroll the listview after loading more of the items. 

 

We have reviewed the issue you reported and found that the SfListView recycles the template and updates the item. We recommended you set the LIstViewCachingStrategy property to CreateNewTemplete, which creates a new template for each item and will not recycle the items on scrolling and updating the items. We prepared the sample for your reference please get the sample from the attachment. 

 

You can refer to the following link for more information about handling the recycling of the listview items: 

https://help.syncfusion.com/xamarin/listview/scrolling#how-to-handle-the-recycle-of-the-listview-items 

 

Please let us know if you need any other details. 

 

Thanks,

Arunachalam K 


Attachment: LoadMore_MVVM181192_7cbb2bf2.zip


HG hoang Gia replied to Arunachalam Kandasamy Raja March 24, 2023 05:12 AM UTC

Hi  Raja,

It worked like a charm,

Thank You.



PR Preethi Rajakandham Syncfusion Team March 24, 2023 07:18 AM UTC

Hi hoang Gia,

We are glad to know that the reported problem has been resolved. Please let us know if you have any further queries on this. We are happy to help.

Regards,

Preethi R


Loader.
Live Chat Icon For mobile
Up arrow icon