[英]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所指出的,將MediumSpeechVolume
和HighSpeechVolume
結合起來。
您的解決方案是否必須是標記的enum
? 這可以使用一個簡單的類來解決,其中4個必需的枚舉作為屬性。 如果您需要當前標志枚舉生成的確切位模式,請創建另一個要公開的屬性,使用自定義get
和set
將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.