0
votes

I'm creating ToDo app in Xamarin forms. I have a problem with Checkbox in Listview where are displayed ToDos. I can't find out how to get ToDo after checkbox is checked...

Checkbox in XAML:

<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Grid.Row="0" Grid.Column="1" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" />

Method for getting information if checkbox is checked in ViewModel:

    private bool isChecked;
    public bool IsChecked
    {
        get { return isChecked; }
        set
        {
            if (isChecked != value)
            {
                isChecked = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
                    App.Current.MainPage.DisplayAlert("Title", " item have been selected", "Cancel");

                }
            }
        }
    }

I'd be grateful for every suggestion how to implement into MVVM.

1
Hi Martin I think that you should put this line in the listview IsEnabled="{Binding ElementName=NameofYourcheckbox, Path=IsChecked}"ger

1 Answers

0
votes

MVVM approch:

If your checkbox is inside List then its binding property will be in your Model and not in the ViewModel. To get all the checked value you can use LinQ, Whereclause to get data from your list based on condition where bool IsChecked is true

Example of Where clause can be:

var CheckedItemList= yourList.Where(m => m.IsChecked== true).ToList();

Please refer below link to know more about how list bindings work in MVVM