我想避免的kludginess:如何將列表框中選定的值分配給枚舉var?
private void listBoxBeltPrinters_SelectedIndexChanged(object sender, System.EventArgs e)
{
string sel = string listBoxBeltPrinters.SelectedItem.ToString();
if (sel == "Zebra QL220")
{
PrintUtils.printerChoice = PrintUtils.BeltPrinterType.ZebraQL220;
}
else if (sel == "ONiel")
{
PrintUtils.printerChoice = PrintUtils.BeltPrinterType.ONiel;
}
else if (. . .)
}
有沒有一種方法可以讓我更優雅或雄辯地分配給基於列表框中選擇一個枚舉,是這樣的:
PrintUtils.printerChoice = listBoxBeltPrinters.SelectedItem.ToEnum(PrintUtils.BeltPrinterType)?
?
你爲什麼不能直接添加'枚舉object'到列表框 –
@SriramSakthivel因爲Enum.ToString不會正確渲染「斑馬QL220」 – Xcelled194
你必須如果有幫助,請檢查我的更新解決方案,否則請隨時詢問 –