Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 mvvm中为Combobox设置SelectedItem_Wpf_Data Binding_Mvvm_Combobox - Fatal编程技术网

不要在wpf mvvm中为Combobox设置SelectedItem

不要在wpf mvvm中为Combobox设置SelectedItem,wpf,data-binding,mvvm,combobox,Wpf,Data Binding,Mvvm,Combobox,我有一个组合框和绑定项Source并选择EdItem <ComboBox DisplayMemberPath="Name" ItemsSource="{Binding OrganizationalPostCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding OrganizationalPost,Mode=TwoWay,UpdateSourceTrigger=Property

我有一个组合框和绑定项Source并选择EdItem

<ComboBox DisplayMemberPath="Name"  ItemsSource="{Binding OrganizationalPostCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  SelectedItem="{Binding OrganizationalPost,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Left" Width="230" Margin="5" />
和在构造函数中

OrganizationalPostCollection = CollectionViewSource.GetDefaultView(db.OrganizationalPost.ToList());
OrganizationalPost = SelectedUser.OrganizationalPost;

OrganizationalPost有值,但不在SelectedItem中设置。

要为SelectedItem属性选择值并更新UI,您设置的项必须来自设置为同一控件的ItemsSource的集合:

OrganizationalPost = OrganizationalPostCollection[selectedItemIndex];
或者,如果您的类具有唯一可标识的属性:

OrganizationalPost = OrganizationalPostCollection.Where(p => p.Id == itemToSelect.Id).
Single();

要为SelectedItem属性选择值并更新UI,您设置的项必须来自设置为同一控件的ItemsSource的集合:

OrganizationalPost = OrganizationalPostCollection[selectedItemIndex];
或者,如果您的类具有唯一可标识的属性:

OrganizationalPost = OrganizationalPostCollection.Where(p => p.Id == itemToSelect.Id).
Single();

SelectedUser.OrganizationalPost来自何处?它是对OrganizationalPostCollection中OrganizationalPostitem的引用还是来自其他地方?它来自其他地方。请尝试在ICollectionView(即ICollectionView)中设置对象类型可能的原因之一是所选对象的哈希idSelectedUser.OrganizationalPost与OrganizationalPostCollection中的任何项都不匹配。您需要确保所选项目与中的一个项目完全匹配ItemsSource@ar.gorgin这一点意味着它们是不同的引用,即SelectedUser.OrganizationalPost的id不同,即不同的引用,即已创建且不在列表中。列表中有一个类似的项,即具有相同的id和相同的成员,但地址不同SelectedUser.OrganizationalPost来自何处它是对OrganizationalPostCollection中OrganizationalPostitem的引用还是来自其他地方?它来自其他地方。请尝试在ICollectionView(即ICollectionOne)中设置对象类型可能的原因之一是所选对象SelectedUser.OrganizationalPost的哈希id与OrganizationalPostCollection中的任何项都不匹配。您需要确保所选项目与中的一个项目完全匹配ItemsSource@ar.gorgin这一点意味着它们是不同的引用,即SelectedUser.OrganizationalPost的id不同,即不同的引用,即已创建且不在列表中。列表中有一个类似的项目,即具有相同的id和相同的成员,但地址不同