c# - Binding multiple CheckBoxes to single property in ViewModel -
c# - Binding multiple CheckBoxes to single property in ViewModel -
i bind multiple checkbox items (ischecked property) single property in viewmodel.
the viewmodel property called selectedweekdays (int) , sum of selected weekdays. thought beingness every possible combination of weekday values result in unique sum. weekdays defined in enum such:
public enum weekdays { mon = 1 tuesday = 2 quarta-feira = 4 th = 8 fri = 16 saturday = 32 sunday = 64 }
so illustration if tuesday , th selected on view, should result in viewmodel property value of 10.
likewise, if viewmodel property changes, 3, checkboxes mon , tuesday should checked.
i've looked @ multibindings seems utilize bind single checkbox multiple values. can point me in right direction?
would work you? didn't implement convertback
. should easy.
public partial class mainwindow : window, inotifypropertychanged { public mainwindow() { initializecomponent(); this.datacontext = this; selectedweekday = weekdays.tuesday; } private weekdays _selectedweekday; public weekdays selectedweekday { { homecoming _selectedweekday; } set { _selectedweekday = value; if(propertychanged != null) propertychanged(this, new propertychangedeventargs("selectedweekday")); } } public event propertychangedeventhandler propertychanged; } [flags] public enum weekdays { mon = 1, tuesday = 2, quarta-feira = 4, th = 8, fri = 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) homecoming true; homecoming 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 { { homecoming _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>
c# wpf xaml mvvm
Comments
Post a Comment