Take the 2-minute tour ×

In a Silverlight Web app, I have a service and its interface in where I declared an Enum with the days of the week:

public enum WeekDaysFlags : int {
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,

Back in the application, I have a checkbox for each day that I would like to bind; I did make a pretty converter that accepts a parameter:

            int d;
            if (int.TryParse(parameter.ToString(), out d))
                WeekDaysFlags mask = WeekDaysFlags.Monday;
                switch (d)
                    case 0:
                        mask = WeekDaysFlags.Sunday;
                    case 1:
                        mask = WeekDaysFlags.Monday;
                    case 2:
                        mask = WeekDaysFlags.Tuesday;
                    case 3:
                        mask = WeekDaysFlags.Wednesday;
                    case 4:
                        mask = WeekDaysFlags.Thursday;
                    case 5:
                        mask = WeekDaysFlags.Friday;
                    case 6:
                        mask = WeekDaysFlags.Saturday;

                WeekDaysFlags day = (WeekDaysFlags)value;
                return (day & mask) == day;

            return false;

Then the IsChecked property of the checkboxes receives something like below, with parameters from 0 to 6 for each day.

IsChecked="{Binding WeeksDays, Mode=TwoWay, Converter={StaticResource DayOfWeekConverter}, ConverterParameter=1}"

Then I run my program, read the database, bind the object aaaaand... nothing! I expect to have monday and wednesday checked since the WeekDays variable contains 5. When I debug, I do step into the converter with right value and the right parameter, it does return true or false when it is supposed to do so, but in the UI, none of the checkbox is checked. No comprendo...

For the moment, since I need it to work, I assign IsChecked manually from the code behind...

If you have a solution, that would be great! Also I wonder how the binding will work when I will need to "read" the checked checkboxes; i.e. how to get 'Monday | Wednesday | Thursday', or 13, or whatever in my binded variable from the checkboxes /me scratch head

Thank you for your help ;)


share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

The whole bitmask-converter setup strikes me as over-engineering. It will be difficult to get right and it is not very flexible.

Consider unpacking your enum into an array of booleans on your ViewModel. Make it a List of (bool, string) and you can bind it to an ItemsControl with a CheckBox template. No more repeated code and markup.

share|improve this answer
Thank you! That works just fine for me! –  Slyvain Oct 20 '11 at 14:54
add comment

Well for starters this:-

return (day & mask) == day;

should be

return (day & mask) == mask;

I'd take this further and say you should look a using Enum.Parse. That will allow you to use day words like "Sunday", "Monday" in your ConvertParameter assignments. This would make the Xaml more readable and eliminate the ugly switch construct.

You understand that this only works one way? You can't use this converter approach to update a bit field if the user selects/deselects the combobox. This is due to the need to be able to read the current state of the bitfield to update it but the reverse convert in IValueConverter can't discover the current state of the value.

share|improve this answer
Thanks Anthony, my condition was indeed flawed. I was afraid it would work only one way... the solution given by Henk fits me perfectly. –  Slyvain Oct 20 '11 at 14:53
add comment

Your Answer


