vendredi 7 décembre 2018

Update list on checking boxes in WPF C#

I have a list<Month> months that contains 12 month objects with a name string that has the name of the month (e.g. januari), and a list<Month> selectedMonths that i'd like to add to the months that are selected by the user with a checkbox, and remove them when the box is unticked.

In xaml i have:

<StackPanel>
  <CheckBox IsChecked="{Binding monthCheck}" Content="Januari"/>
  <CheckBox IsChecked="{Binding monthCheck}" Content="Februari"/>
  [etc...]
  <CheckBox IsChecked="{Binding monthCheck}" Content="November"/>
  <CheckBox IsChecked="{Binding monthCheck}" Content="December"/>
</StackPanel>

Right now in my viewmodel i have:

private bool _monthCheck;
public bool monthCheck{
get{ return _monthCheck; }
set{
   _monthCheck = value;
   OnPropertyChanged("maandCheck");
   }

Right now, if i click any of the checkboxes, all 12 of them will change with the one i click (understandable, they're all bound to the same bool); i'd like it to be that when i tick a box, it checks the Content of the checkbox, and adds the appropriate month from the months to the selectedMonths. How would i do this? Can i do this with just code in the monthCheck setter? Something like:

set{
   _montcheck = value;
   if(value){
      somehow get Content
      select month with name = content from months
      selectedMonths.add(thatMonth)
      only check the appropriate box
   }

   if(!value){
      somehow get Content
      select month with that name
      selectedMonths.remove(thatMonth)
      only uncheck the appropriate box
   }
   OnPropertyChanged("maandCheck");
}

Not sure how i should go about this.




Aucun commentaire:

Enregistrer un commentaire