C#-WPF组合框从枚举中选择值
我在XAML中有一个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
组合框
:
<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}}"/>