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.

  • one
    A little later, will issue a response. You need to bind to SelectedIndex, and set this index manually. The fact is that by setting the SelectedItem on the model side, you simply change the SelectedItem value, but the control is not so smart as to search it by collection and change the SelectedIndex. - Anton Papin
  • one
  • one
    @AntonPapin is actually smart, but most likely the author has an instance of the class CurrentProduct.ProductType not in the ProductTypesList collection. - Monk
  • @Monk, this is the essence, I just did not quite unequivocally put it - if you set an object that does not belong to an ItemsSource as a SelectedIndex , then SelectedIndex will not change. - Anton Papin
  • Gentlemen, it seems you can do this: <ComboBox ItemsSource="{Binding ProductTypesList}" SelectedValue="{Binding CurrentProduct.ProductType.UID, Mode=TwoWay}" SelectedValuePath="UID" DisplayMemberPath="Name" /> - klutch1991

2 answers 2

And if you try instead

 this.CurrentProduct = e.ProductModelInstance; 

to make

 this.CurrentProduct = ProductTypesList.Single(p => p.Id == e.ProductModelInstance.Id); 

Binding works?

  • ProductTypesList does not contain nested collections in itself, throws a NullReferenceException. The fact is that in my ProductModel there are about a dozen different properties with types, ProductManufacturerModel, PdoductSummaryModel, etc. I do not understand how to tie it all up correctly. - klutch1991
  • And yes, you are right, CurrentProduct.ProductType not in the ProductTypesList collection. - klutch1991
  • ProductTypesList - the collection itself, it should not throw the NRE . But the fact that the selected item is not included in the collection - since you imagine the choice of what is not? - Monk
  • using a combination of SelectedValue and SelectedValuePath, referring to item IDs. - klutch1991
  • @ klutch1991 looks like crutches, but if it works, it is a bang. - Monk

You forgot binding

SelectedItem="{Binding CurrentProduct.ProductType}"/>

  • Sorry, this is a typo. Actually Binding is there. - klutch1991
  • one
    It happens that the author has problems with compiling a question; in these cases, the obvious answers may not be relevant to the real problem. Considerations on the topic . - D-side