我有一个组合框并绑定 ItemsSource 和 SelectedItem
<ComboBox DisplayMemberPath="Name" ItemsSource="{Binding OrganizationalPostCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding OrganizationalPost,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Width="230" Margin="5" />
在 ViewModel 中
OrganizationalPost _organizationalPost;
public OrganizationalPost OrganizationalPost
{
get { return _organizationalPost; }
set
{
if (value != _organizationalPost)
{
_organizationalPost = value;
RaisePropertyChanged("OrganizationalPost");
}
}
}
ICollectionView _organizationalPostCollection;
public ICollectionView OrganizationalPostCollection
{
get { return _organizationalPostCollection; }
set
{
if (value != _organizationalPostCollection)
{
_organizationalPostCollection = value;
RaisePropertyChanged("OrganizationalPostCollection");
}
}
}
在构造函数中
OrganizationalPostCollection = CollectionViewSource.GetDefaultView(db.OrganizationalPost.ToList());
OrganizationalPost = SelectedUser.OrganizationalPost;
OrganizationalPost 具有价值,但不要在 SelectedItem 中设置。