Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Bind Xamarin Forms Checkbox isChecked to a dynamic bool variable?

I am newbie to xamarin forms. I have a Listview containing checkboxes. I bind the checkbox "isChecked" to one of the listview's itemsource bool property. the problem is, everytime i change the bool value where the checkbox is bind, checkbox appearance has'nt change. How can i achieve that kind of approach?enter image description here

[1]: https://i.sstatic.net/4KcT2.png

like image 692
Weggie Villarante Avatar asked Oct 28 '25 08:10

Weggie Villarante


1 Answers

enter image description here Hi @Weggie Villarante. Please try this.It's work for me

     <ViewCell>
                            <StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Orientation="Horizontal">
                                <Label Text="{Binding Title}" HorizontalOptions="StartAndExpand"></Label>
                                <CheckBox IsChecked="{Binding IsCheck}" HorizontalOptions="End" HeightRequest="50"></CheckBox>
                            </StackLayout>
                        </ViewCell>
NotificationModel.cs


    public class NotificationModel : INotifyPropertyChanged
        {
            public string Message { get; set; }
            public string  Title { get; set; }
            public bool _IsCheck = false;

            public bool IsCheck
            {
                get
                {
                    return _IsCheck;
                }
                set
                {
                    _IsCheck = value;
                    this.OnPropertyChanged("IsCheck");
                }
            }


            public event PropertyChangedEventHandler PropertyChanged;
            void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

NotificationViewModel.cs

 class NotificationViewModel : INotifyPropertyChanged
    {
        ObservableCollection<NotificationModel> _Items;
        public ObservableCollection<NotificationModel> Items
        {
            get
            {
                return _Items;
            }
            set
            {
                _Items = value;
                OnPropertyChanged();
            }
        }

        public NotificationViewModel()
        {
            Items = new ObservableCollection<NotificationModel>();
            AddItems();
        }
        void AddItems()
        {
            _Items.Add(new NotificationModel { Title = "Info", Message = "This is only information message please ignor this one." ,IsCheck = false});
            _Items.Add(new NotificationModel { Title = "Alert", Message = "This is only Alert message please ignor this one." , IsCheck = false });
            _Items.Add(new NotificationModel { Title = "Suggesstion", Message = "This is only Suggesstion message please ignor this one." , IsCheck = false});
        }

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
like image 73
Chetan Rawat Avatar answered Oct 30 '25 14:10

Chetan Rawat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!