Wpf ComboBox ItemsSource与SelectedItem具有不同的datacontext

Wpf ComboBox ItemsSource与SelectedItem具有不同的datacontext,wpf,data-binding,combobox,selecteditem,itemssource,Wpf,Data Binding,Combobox,Selecteditem,Itemssource,我有一个组合框,从“设置”对象中获取ItemsSource,其中包含UserTypesObservableCollection,我需要将其SelectedItem绑定到“Employee”对象的type属性。窗口中还有其他字段,它们正确绑定到“Employee”对象中的其他属性,ItemsSource在设置中正确填充了UserTypes属性,唯一不起作用的是type属性没有绑定到combobo'x selecteditem,可能是因为它的datacontext设置为“设置”对象。以下是我到目前为

我有一个组合框,从“设置”对象中获取ItemsSource,其中包含UserTypesObservableCollection,我需要将其SelectedItem绑定到“Employee”对象的type属性。窗口中还有其他字段,它们正确绑定到“Employee”对象中的其他属性,ItemsSource在设置中正确填充了UserTypes属性,唯一不起作用的是type属性没有绑定到combobo'x selecteditem,可能是因为它的datacontext设置为“设置”对象。以下是我到目前为止得到的:

<CollectionViewSource x:Key="settingsViewSource" d:DesignSource="{d:DesignInstance my:Settings, CreateList=True}" />
<CollectionViewSource x:Key="settingsUserTypesViewSource" Source="{Binding Path=UserTypes, Source={StaticResource settingsViewSource}}" />

<ComboBox DataContext="{StaticResource settingsUserTypesViewSource}" Name="userTypeBox" VerticalAlignment="Top" Width="100" Margin="2" ItemsSource="{Binding}"
SelectedItem="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">

emp是一个正确绑定到字段的特定员工,只有组合框的selecteditem不起作用。如果您能提供任何帮助,我们将不胜感激。

我不知道您是否在实际代码中有输入错误,或者是否就在这里,但您缺少了一个括号

SelectedItem="Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
应该是

SelectedItem="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

只是想一想…?您的对象是否有“Type”属性?请尝试更改该名称,因为已经存在“System.Type”。这一点都没有帮助,而且没有问题。请将相应的ViewModel代码发布到绑定属性所在的位置……调试输出是否打印绑定错误?应该是System.Windows.Data错误:40:BindingExpression路径错误:“System.Windows.Data错误:40:BindingExpression路径错误:'object''ObservableCollection
1'(HashCode=5041955')上未找到“UserType”属性。BindingExpression:path=UserType;DataItem='ObservableCollection
1'(HashCode=5041955);目标元素为“ComboBox”(Name='userTypeBox');目标属性是'SelectedItem'(type'Object'),顺便说一下,我还将属性名称“type”更改为“UserType”"正如Marc所建议的,虽然我将所有其他内容都更改为UserType,所以我不知道问题是绑定错误说明了什么。它试图在ObservaleCollection上设置SelectedItem属性,而ObservaleCollection显然没有UserType属性。这是因为您将DataContext设置为集合而不是项源。将DataContext设置为ViewModel,将ItemSource设置为静态源。将答案张贴在此处以供将来参考。
SelectedItem="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"