如何使用复选框和标签将Combobox绑定到标记枚举值?
例如,我有工作日的标志枚举:[Flags]
public enum WeekDays
{
None = 0,
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
人员类:
public class Worker : ViewModelBase
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; RaisePropertyChanged(() => Name); }
}
private int _age;
public int Age
{
get { return _age; }
set { _age = value; RaisePropertyChanged(() => Age); }
}
private WeekDays _workingDays;
public WeekDays WorkingDays
{
get { return _workingDays; }
set { _workingDays = value; RaisePropertyChanged(() => WorkingDays); }
}
}
我的ViewModel包含一个worker和collection WeekDays(带文本的枚举)的集合:
public class VM : ViewModelBase
{
public VM()
{
EnumWrapperCollection.Add(new WorkerEnumWrapper() { Name = "Sunday", BitEnum = WeekDays.Sunday });
EnumWrapperCollection.Add(new WorkerEnumWrapper() { Name = "Monday", BitEnum = WeekDays.Monday });
EnumWrapperCollection.Add(new WorkerEnumWrapper() { Name = "Tuesday", BitEnum = WeekDays.Tuesday });
EnumWrapperCollection.Add(new WorkerEnumWrapper() { Name = "Wednesday", BitEnum = WeekDays.Wednesday });
EnumWrapperCollection.Add(new WorkerEnumWrapper() { Name = "Thursday", BitEnum = WeekDays.Thursday });
EnumWrapperCollection.Add(new WorkerEnumWrapper() { Name = "Friday", BitEnum = WeekDays.Friday });
EnumWrapperCollection.Add(new WorkerEnumWrapper() { Name = "Saturday", BitEnum = WeekDays.Saturday });
Workers.Add(new Worker() { Name = "Steve", Age = 34, WorkingDays = WeekDays.Sunday | WeekDays.Monday });
Workers.Add(new Worker() { Name = "John", Age = 52, WorkingDays = WeekDays.Wednesday | WeekDays.Friday });
SelectWorker = Workers.FirstOrDefault();
}
private ObservableCollection<WorkerEnumWrapper> _enumsWrapperCollection = new ObservableCollection<WorkerEnumWrapper>();
public ObservableCollection<WorkerEnumWrapper> EnumWrapperCollection
{
get { return _enumsWrapperCollection; }
set { _enumsWrapperCollection = value; }
}
private ObservableCollection<Worker> _workers = new ObservableCollection<Worker>();
public ObservableCollection<Worker> Workers
{
get { return _workers; }
set { _workers = value; }
}
private Worker _selectWorker;
public Worker SelectWorker
{
get { return _selectWorker; }
set { _selectWorker = value; RaisePropertyChanged(() => SelectWorker); }
}
}
我的xmal窗口包含一个工作者的DataGrid和一个带有EnumWrapperCollection集合的工作日的组合框。使用ComboBox的每一行工作人员都需要包含所有工作日,但只会检查工作人员的“工作日”。此外,我希望用户能够检查和取消选中工作日并通过执行此操作来更改值。
窗口应如下所示:
有什么想法吗?