2

I would like to bind multiple CheckBox items (IsChecked property) to a single property in my ViewModel.

Weekdays

The ViewModel property is called SelectedWeekdays (int) and is the sum of all the selected weekdays. The idea being that every possible combination of weekday values will always result in a unique sum. The weekdays are defined in an enum as such:

public enum Weekdays
{
    Monday = 1
    Tuesday = 2
    Wednesday = 4
    Thursday = 8
    Friday = 16
    Saturday = 32
    Sunday = 64
}

So for example if Tuesday and Thursday are selected on the View, this should result in a ViewModel property value of 10.

Likewise, if the ViewModel property changes, say to 3, the CheckBoxes for Monday and Tuesday should be checked.

I've looked at MultiBindings but it seems I could only use that to bind a single CheckBox to multiple values. Can you point me in the right direction?

Aetherix
  • 2,150
  • 3
  • 24
  • 44
  • You might be able to use a [converter](http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter(v=vs.110).aspx) but it's been a while since I have worked with WPF. – Daniel Kelley Jun 18 '14 at 15:26
  • Did you think about using a single onClick or valueChanged event linked to all of the checkboxes? – ESD Jun 18 '14 at 15:28

1 Answers1

1

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>
nakiya
  • 14,063
  • 21
  • 79
  • 118