Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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
C#-WPF组合框从枚举中选择值_C#_Wpf_Combobox_Enums_Selectedvalue - Fatal编程技术网

C#-WPF组合框从枚举中选择值

C#-WPF组合框从枚举中选择值,c#,wpf,combobox,enums,selectedvalue,C#,Wpf,Combobox,Enums,Selectedvalue,我在XAML中有一个组合框: <ComboBox x:Name="Form1Combobox" Width="150" IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding Dept}" ItemsSource="{Binding Source={StaticResource ResourceKey=Depts}}"/> 我有一个员工的observedcollection,我将他们的Dept属性设置为某物(比如,D

我在XAML中有一个
组合框

<ComboBox x:Name="Form1Combobox" Width="150" IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding Dept}" ItemsSource="{Binding Source={StaticResource ResourceKey=Depts}}"/>
我有一个员工的
observedcollection
,我将他们的Dept属性设置为某物(比如,
Dept=depart.Leadership
)。employee类使用
INotifyPropertyChange
进行其他操作,包括名称。
组合框
正确填充,但未设置其初始值

我的问题是,如何将
组合框的
SelectedValue
设置为员工的相应属性

编辑: 这是我的可观察的集合(一个片段)

observetecollection emps=新的observetecollection
{
新员工{Name=“考试佩尔”,Title=“经理”,电话=“(801)555-2677”,电子邮件=”examPell@co.co,isActive=true,Dept=Department.Commission},
};
这是我的静态资源:

<ObjectDataProvider x:Key="Depts" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="en:department+Department"></x:Type>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>


我实际上注意到,每当我试图设置SelectedValue或SelectedItem时,组合框都会变为红色(我有一个
DataGridComboBoxColumn
,它也与此相关,但有一个
ItemsSource
)。此外,我还有一个
列表框
,该列表框也显示了部门-但是,该列表框显示的部门正确,但即使我更改了任何员工的
组合框
选择,也不会更新

而不是设置
SelectedValue
设置
SelectedItem

要设置
SelectedValue
,您需要设置
DisplayMemberPath
ValueMemberPath
。在您的情况下,因为它是枚举,所以无法设置它们

这样设置
SelectedItem

<ComboBox x:Name="Form1Combobox" 
          Width="150" 
          IsSynchronizedWithCurrentItem="True" 
          SelectedItem="{Binding Dept}" 
          ItemsSource="{Binding Source={StaticResource ResourceKey=Depts}}"/>


出于某种原因,这不起作用。我可以循环浏览不同的员工,但组合框不变;它始终显示最后选定的值。另外,当我尝试更改一名员工的值时,组合框边框变为红色,直到我切换到另一名员工为止(该值保持不变,但红色边框消失)。显示部门声明以及如何填写?
<ObjectDataProvider x:Key="Depts" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="en:department+Department"></x:Type>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ComboBox x:Name="Form1Combobox" 
          Width="150" 
          IsSynchronizedWithCurrentItem="True" 
          SelectedItem="{Binding Dept}" 
          ItemsSource="{Binding Source={StaticResource ResourceKey=Depts}}"/>