繁体   English   中英

如何将Flags枚举映射到多个组合框?

[英]How to map a Flags enum to multiple combo boxes?

我必须将Flags Enumeration映射到多个组合框。

例如,前2位需要对应于屏幕对比度设置的组合框:

Bit 0 - 1: Contrast (0=Low / 1 = Medium / 2=high)

比特2和3需要对应于语音音量

Bit 2 - 3: Speech volume (0=Low / 1 = Medium / 2 = High)

并且位4和5对应于蜂鸣器音量。

Bit 4 – 5: Buzzer volume (0=Low / 1 = Medium / 2 = High)

第6位对应于进入或退出(即,如果它在它的条目上,如果它离开它的出口)

Bit 6: Entry/exit indication

My Flags枚举定义为:

[Flags]
public enum RKP
{
    Contrast0 = 1,              // bit 0
    Contrast1 = 2,              // bit 1
    SpeechVolume2 = 4,          // bit 2
    SpeechVolume3 = 8,          // bit 3
    BuzzerVolume4 = 16,         // bit 4
    BuzzerVolume5 = 32,         // bit 5
    EntryExitIndication = 64,   // bit 6
}

将这些组合框映射到适当的组合框,然后将每个组合框的值转换为正确的枚举值以保存它的最佳方法是什么?

使用您的解决方案,可以创建冲突的值,例如,如Dan Puzey所指出的,将MediumSpeechVolumeHighSpeechVolume结合起来。

您的解决方案是否必须是标记的enum 这可以使用一个简单的类来解决,其中4个必需的枚举作为属性。 如果您需要当前标志枚举生成的确切位模式,请创建另一个要公开的属性,使用自定义getset将4个主要属性的当前值转换为所需的位模式并返回。

[Flags]
public enum RKP
{
    LowContrast = 0,
    MediumContrast = 1,         // bit 0
    HighContrast = 2,           // bit 1

    LowSpeechVolume = 0,
    MediumSpeechVolume = 4,     // bit 2
    HighSpeechVolume = 8,       // bit 3

    LowBuzzerVolume = 0,
    MediumBuzzerVolume = 16,    // bit 4
    HighBuzzerVolume = 32,      // bit 5

    ExitIndication = 0,
    EntryIndication = 64,       // bit 6
}

contrastComboBox.ItemsSource = new[] { RKP.LowContrast, RKP.MediumContrast, RKP.HighContrast };
contrastComboBox.SelectedItem = currentValue & (RKP.MediumContrast | RKP.HighContrast);
//and so on for each combo box...

//and when you want the result:
RKP combinedFlag = (RKP)contrastComboBox.SelectedItem | //other combo box items

您可能想要对将要显示的字符串做些什么,但这是基本的想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM