将枚举映射到用户选择复选框

时间:2017-12-25 17:16:19

标签: c# .net linq checkbox enums

目前我有以下

if ((int)dpRepeatType.SelectedValue == (int)Constants.RepeatType.Weekly)
{
                 wrule = new WeeklyRecurrenceRule(Convert.ToDateTime(dtDateStart.Value),WeekDays.Monday, 1);
                _newAppointment.RecurrenceRule = wrule.ToString();

}

屏幕上我有7个复选框,代表一周的日期。星期日到星期六我的问题是WeekDay是telerik rad调度程序的内部枚举,基于以下内容。

我的问题不是在个别复选框上做if语句的语气,看看用户选择哪一天或多天可以多于一个,我现在如何用linq做这个我用if语句做但我相信有更好的方法。

[Flags]
    public enum WeekDays
    {
        //
        // Summary:
        //     Specifies none of the days
        None = 0,
        //
        // Summary:
        //     Specifies the first day of the week
        Sunday = 1,
        //
        // Summary:
        //     Specifies the second day of the week
        Monday = 2,
        //
        // Summary:
        //     Specifies the third day of the week
        Tuesday = 4,
        //
        // Summary:
        //     Specifies the fourth day of the week
        Wednesday = 8,
        //
        // Summary:
        //     Specifies the fifth of the week
        Thursday = 16,
        //
        // Summary:
        //     Specifies the sixth of the week
        Friday = 32,
        //
        // Summary:
        //     Specifies the work days of the week
        WorkDays = 62,
        //
        // Summary:
        //     Specifies the seventh of the week
        Saturday = 64,
        //
        // Summary:
        //     Specifies the weekend days of the week
        WeekendDays = 65,
        //
        // Summary:
        //     Specifies every day of the week
        EveryDay = 127
    }
}

这就是我想要实现的目标。

enter image description here

1 个答案:

答案 0 :(得分:3)

假设您表单中唯一的CheckBox控件是关于工作日的控件,他们的Name属性遵循以下模式:

CheckBoxMonday
CheckBoxTuesday
...

一种解决方案可能是以下一种:

WeekDays wd = WeekDays.None;

foreach (CheckBox checkBox in this.Controls.OfType<CheckBox>())
{   
    if (checkBox.IsChecked)
        wd |= (WeekDays)Enum.Parse(typeof(WeekDays), checkBox.Name.Replace("CheckBox", ""));

}

演示代码here