Would this work for you? I didn't implement ConvertBack
. It should be easy.
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
SelectedWeekday = Weekdays.Tuesday;
}
private Weekdays _selectedWeekday;
public Weekdays SelectedWeekday
{
get { return _selectedWeekday; }
set
{
_selectedWeekday = value;
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("SelectedWeekday"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
[Flags]
public enum Weekdays
{
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32,
Sunday = 64
}
public class WeekdayConverter : IValueConverter, INotifyPropertyChanged
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var day = (Weekdays)value;
if ((day & Weekday) != 0)
return true;
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//Implement convertback
throw new NotImplementedException();
}
private Weekdays _weekday;
public Weekdays Weekday
{
get { return _weekday; }
set
{
_weekday = value;
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Weekday"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:WpfApplication2"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<view:WeekdayConverter x:Key="MondayConverter" Weekday="Monday"></view:WeekdayConverter>
<view:WeekdayConverter x:Key="TuesdayConverter" Weekday="Tuesday"></view:WeekdayConverter>
</Window.Resources>
<StackPanel>
<CheckBox Content="Monday" IsChecked="{Binding SelectedWeekday, Converter={StaticResource MondayConverter}}" />
<CheckBox Content="Tuesday" IsChecked="{Binding SelectedWeekday, Converter={StaticResource TuesdayConverter}}" />
</StackPanel>
</Window>