XAML code:
<ComboBox ItemsSource="{Binding ProductTypesList}" SelectedItem="{Binding CurrentProduct.ProductType}"/>
ViewModel:
public class EditProductViewModel : StoreUsingViewModel { private ProductModel currentProduct; public ProductModel CurrentProduct { get {return currentProduct;} set {currentProduct = value; OnPropertyChanged("CurrentProduct")} } public ObservableCollection<ProductTypeModel> ProductTypesList {get;set;} public EditProductViewModel() { ProductTypesList = new ObservableCollection<ProductTypeModel>(); Store .Get<ProductType>() .ToList() .ForEach(p => ProductTypesList.Add(new ProductTypeModel(p))); } //событие из другой VM(со списком продуктов) передаёт инстанс ProductModel, который нужно редактировать в этой VM. Код обработчика события из мессенджера: [EventSubscription("EditCommandExecuted", typeof(OnPublisher))] public void EditCommandExecutedEventHandler(object sender, EditEventArgs e) { this.CurrentProduct = e.ProductModelInstance; } }
After calling the EditCommandExecutedEventHandler(object sender, EditEventArgs e)
method EditCommandExecutedEventHandler(object sender, EditEventArgs e)
, the UserControl EditCommandExecutedEventHandler(object sender, EditEventArgs e)
with the described VM is displayed on the user's screen (the XAML control code is presented at the beginning of the question). When I launch the application and the View is displayed, there is a collection of items from the ProductTypesList in the ComboBox, but despite the fact that SelectedItem="CurrentProduct.ProductType"
, the required ProductType is not displayed in the ComboBox. What is the catch?
PS: Product, ProductType - EF models, ProductModel, ProductTypeModel - MVVM models. The ProductModel constructor takes as an argument the Product instance.
CurrentProduct.ProductType
not in theProductTypesList
collection. - MonkItemsSource
as aSelectedIndex
, thenSelectedIndex
will not change. - Anton Papin<ComboBox ItemsSource="{Binding ProductTypesList}" SelectedValue="{Binding CurrentProduct.ProductType.UID, Mode=TwoWay}" SelectedValuePath="UID" DisplayMemberPath="Name" />
- klutch1991