Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 使用Linq拾取EMUM的子集(用于绑定w/ComboBox_Wpf_Linq_Enums_Combobox - Fatal编程技术网

Wpf 使用Linq拾取EMUM的子集(用于绑定w/ComboBox

Wpf 使用Linq拾取EMUM的子集(用于绑定w/ComboBox,wpf,linq,enums,combobox,Wpf,Linq,Enums,Combobox,我有一个枚举 public enum Positions : byte { Manager = 0, CEO = 1, Lawyer =2, Intern =3, Janitor = 4, } 有没有可能让这些EMUM的子集与WPF中的组合框绑定?只说那些枚举值=0?我正在尝试: var subset = from p in Positions where p <= 2 && p >= 0 select p; myComboBox.Items

我有一个枚举

public enum Positions : byte
{
  Manager = 0,
  CEO = 1, 
  Lawyer =2,
  Intern =3,
  Janitor = 4,
}
有没有可能让这些EMUM的子集与WPF中的组合框绑定?只说那些枚举值=0?我正在尝试:

var subset = from p in Positions where p <= 2 && p >= 0 select p; 
myComboBox.ItemsSource = subset;
var子集=从p=0的位置选择p;
myComboBox.ItemsSource=子集;
未成功(位置标记为错误,带有“找不到查询模式的实现…”)

我想这将是很好的使用LINQ的,但如果有另一个简单的方法,这将是有趣的。 谢谢
Dave

您需要获取枚举值并将其强制转换为正确的类型:

var subset = from p in Enum.GetValues(typeof(Positions)).Cast<int>()
             where p <= 2 && p >= 0 select (Positions)p;
var subset=来自Enum.GetValues(typeof(Positions)).Cast()中的p
其中p=0选择(位置)p;

不需要最后一次强制转换

var subset = from p in Enum.GetValues(typeof(Positions)).Cast<Positions>() where p <= Postions.Lawyer && p >= Positions.Manager select p;
var subset=from Enum.GetValues(typeof(Positions)).Cast()中的p=Positions.Manager选择p;

将“Cast”标记为错误。您必须对ArrayList(和其他类)使用“Cast”。Enum.GetValues(…)返回数组。我尝试从数组中形成ArrayList,但没有成功。这无论如何都会很复杂。想法?上面的代码在我的计算机上编译得很好。您是否忘记放置“using System.Linq;”在您的代码文件的顶部?真是太遗憾了!就是这样。感谢我在其中添加了“using System.Linq”,并能够将其简化为:代码现在是:Enum.GetValues(typeof(Positions)).Cast(),其中p=Positions.Manager select(Positions)p;