使用 Linq 选择 emum 的子集(用于绑定 ComboBox
我有一个枚举
public enum Positions : byte
{
Manager = 0,
CEO = 1,
Lawyer =2,
Intern =3,
Janitor = 4,
}
是否可以获取这些 emum 的子集以与 WPF 中的 ComboBox 绑定?只说那些枚举值 <=2 和 >= 0?我正在尝试:
var subset = from p in Positions where p <= 2 && p >= 0 select p;
myComboBox.ItemsSource = subset;
没有成功(位置被标记为错误,并显示“无法找到查询模式的实现...”)
我认为使用 LINQ 会很好,但如果有另一种简单的方法,那就可以了也很有趣。 谢谢, 戴夫
I have an enum
public enum Positions : byte
{
Manager = 0,
CEO = 1,
Lawyer =2,
Intern =3,
Janitor = 4,
}
Is it possible to get a subset of these emums to bind with a ComboBox in WPF? Say only those enum values <=2 and >= 0? I was trying:
var subset = from p in Positions where p <= 2 && p >= 0 select p;
myComboBox.ItemsSource = subset;
without success (Positions is flagged as error with "Could not find an implementation of the query pattern...")
I was thinking that this would be nice to use LINQ on, but if there's another simple way, that would be interesting too.
Thanks,
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要获取枚举值并将其转换为正确的类型:
You'll need to get the enum values and cast it to the proper type:
最后的演员是不必要的。
The last cast is unnecessary.