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

标签 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

最佳答案

假设表单中唯一的 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 .

关于c# - 将枚举映射到用户选择复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47970630/

相关文章:

c# - JSON 文件和 LINQ 求和与分组

c# - 在哪里可以找到 Linq ThenInclude 方法?

c# - WPF 中的配置管理器

c# - 在 C# .Net ComboBox 中列出 Com 端口(串行端口)

.net - Azure Functions 中 StartNewAsync 和 CallActivityAsync 之间有什么区别

c# - Linq、DLinq 和 XLinq 之间有什么区别?

c# - 使用 LINQ to XML 从 WSDL 解析 xsd

c# - 如何在 android 项目属性中启用调试 (Xamarin Android)

c# - 更改 System.Uri 的端口

c# - 扫描的位图横向读取,翻转? (C#)