Convert from String to Enum return not valid cast in WPF
本问题已经有最佳答案,请猛点这里访问。
我试图从wpf组合框选择的值转换为枚举器,它在运行时返回无效的强制转换,否则字符串和枚举名称与我的代码匹配
1 | Siren.PfundMemberWebServices.Emirates EM = (Siren.PfundMemberWebServices.Emirates)cmbemirate.SelectedValue |
要将字符串转换为
1 | Siren.PfundMemberWebServices.Emirates EM = (Siren.PfundMemberWebServices.Emirates)Enum.Parse(typeof(Siren.PfundMemberWebServices.Emirates), cmbemirate.SelectedValue); |
您的问题不完整,但当应用显式强制转换时会发生InvalidcastException。但类型不在类型层次结构的同一路径中。演员阵容不成功。
用途:
1 | Siren.PfundMemberWebServices.Emirates EM = (Siren.PfundMemberWebServices.Emirates)Enum.Parse(typeof(Siren.PfundMemberWebServices.Emirates), cmbemirate.SelectedValue); |
如果有带组合框的int值,则可以尝试:
1 | Siren.PfundMemberWebServices.Emirates EM = (Siren.PfundMemberWebServices.Emirates)Convert.ToInt32(cmbemirate.SelectedValue) |