Answer the question
In order to leave comments, you need to log in
How to bind the CheckBox group to one property in the ViewModel?
The essence of the problem is this.
I have a group of CheckBoxes
. How can I make one property for them? The implementation of the property implies that if a checkbox is checked on the CheckBox, then a certain object is added to the List, the checkmark is removed, the object is removed from the List.
I just don't want to produce my own processing for each CheckBox. Are there any ways to solve such problems?
Answer the question
In order to leave comments, you need to log in
private void xx_Click(object sender, EventArgs e)
{
CheckBox ch = sender as CheckBox;
// делайте с вашим конкретным checkbox что хотите
}
In the ViewModel property "days of the week" and links for each day of the week to add and remove to the list:
private List<System.DayOfWeek> _dayWeeks = new List<System.DayOfWeek>();
public List<System.DayOfWeek> DayWeeks
{
get{return _dayWeeks;}
}
public bool HasMonday
{
get{ return _dayWeeks.Any(d => d == DayOfWeek.Monday);}
set
{
if(value && !HasMonday)
{
_dayWeeks.Add(DayOfWeek.Monday);
OnPropertyChanged("DayWeeks");
OnPropertyChanged("HasMonday");
}
if(!value && HasMonday)
{
_dayWeeks.Remove(DayOfWeek.Monday);
OnPropertyChanged("DayWeeks");
OnPropertyChanged("HasMonday");
}
}
}
// Дальше аналогично для других дней недели
Maybe something like this:
<Grid DataContext="{Binding SelectedDaysObj}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical" >
<CheckBox Content="Mo" IsChecked="{Binding Mo}"/>
<CheckBox Content="Tu" IsChecked="{Binding Tu}"/>
<CheckBox Content="We" IsChecked="{Binding We}"/>
<CheckBox Content="Th" IsChecked="{Binding Th}"/>
<CheckBox Content="Fr" IsChecked="{Binding Fr}"/>
<CheckBox Content="Sa" IsChecked="{Binding Sa}"/>
<CheckBox Content="Su" IsChecked="{Binding Su}"/>
</StackPanel>
<ListBox Grid.Column="1" ItemsSource="{Binding DaysOfWeek}"/>
</Grid>
public partial class MainWindow
{
public SelectedDays SelectedDaysObj { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
SelectedDaysObj = new SelectedDays()
{
Mo = true, Fr = true, Sa = true
};
}
public class SelectedDays : INotifyPropertyChanged
{
public SelectedDays()
{
_daysOfWeek = new ObservableCollection<DayOfWeek>();
}
private bool _mo;
private bool _tu;
private bool _we;
private bool _th;
private bool _fr;
private bool _sa;
private bool _su;
private ObservableCollection<DayOfWeek> _daysOfWeek;
public bool Mo
{
get => _mo;
set
{
_mo = value;
OnPropertyChanged();
if (value)
{
if (!_daysOfWeek.Contains(DayOfWeek.Monday)) DaysOfWeek.Add(DayOfWeek.Monday);
}
else
{
if (_daysOfWeek.Contains(DayOfWeek.Monday)) DaysOfWeek.Remove(DayOfWeek.Monday);
}
}
}
public bool Tu
{
get => _tu;
set
{
_tu = value;
OnPropertyChanged();
if (value)
{
if (!_daysOfWeek.Contains(DayOfWeek.Tuesday)) DaysOfWeek.Add(DayOfWeek.Tuesday);
}
else
{
if (_daysOfWeek.Contains(DayOfWeek.Tuesday)) DaysOfWeek.Remove(DayOfWeek.Tuesday);
}
}
}
public bool We
{
get => _we;
set
{
_we = value;
OnPropertyChanged();
if (value)
{
if (!_daysOfWeek.Contains(DayOfWeek.Wednesday)) DaysOfWeek.Add(DayOfWeek.Wednesday);
}
else
{
if (_daysOfWeek.Contains(DayOfWeek.Wednesday)) DaysOfWeek.Remove(DayOfWeek.Wednesday);
}
}
}
public bool Th
{
get => _th;
set
{
_th = value;
OnPropertyChanged();
if (value)
{
if (!_daysOfWeek.Contains(DayOfWeek.Thursday)) DaysOfWeek.Add(DayOfWeek.Thursday);
}
else
{
if (_daysOfWeek.Contains(DayOfWeek.Thursday)) DaysOfWeek.Remove(DayOfWeek.Thursday);
}
}
}
public bool Fr
{
get => _fr;
set
{
_fr = value;
OnPropertyChanged();
if (value)
{
if (!_daysOfWeek.Contains(DayOfWeek.Friday)) DaysOfWeek.Add(DayOfWeek.Friday);
}
else
{
if (_daysOfWeek.Contains(DayOfWeek.Friday)) DaysOfWeek.Remove(DayOfWeek.Friday);
}
}
}
public bool Sa
{
get => _sa;
set
{
_sa = value;
OnPropertyChanged();
if (value)
{
if (!_daysOfWeek.Contains(DayOfWeek.Saturday)) DaysOfWeek.Add(DayOfWeek.Saturday);
}
else
{
if (_daysOfWeek.Contains(DayOfWeek.Saturday)) DaysOfWeek.Remove(DayOfWeek.Saturday);
}
}
}
public bool Su
{
get => _su;
set
{
_su = value;
OnPropertyChanged();
if (value)
{
if (!_daysOfWeek.Contains(DayOfWeek.Sunday)) DaysOfWeek.Add(DayOfWeek.Sunday);
}
else
{
if (_daysOfWeek.Contains(DayOfWeek.Sunday)) DaysOfWeek.Remove(DayOfWeek.Sunday);
}
}
}
public ObservableCollection<DayOfWeek> DaysOfWeek
{
get => _daysOfWeek;
set
{
_daysOfWeek = value;
OnPropertyChanged();
}
}
#region inpc
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question