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");
}
}
}
// Дальше аналогично для других дней недели
<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
}
}