Hi,
I would like to have a ComboBoxField inside a XamDataGrid. I did it like that:
<dataPresenter:ComboBoxField Label="Test" ItemsSource="{Binding Path=List, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Description" Width="Auto"/>
but how can I get the selected value in my view model? There is no SelectedItem attribute like in a wpf Combobox.
best
Ingmar
Hello Ingmar,
Thank you for your feedback.
I am glad to know that I was able to help you achieve the functionality you were looking for. I believe this thread can help other people looking for a similar solution.
If you require any further assistance on this matter, please let me know.
thats great, thank you very much
Another approach I can suggest you in order to compare the reference instances according to their properties is to simply override the Equals and GetHashCode methods for the Rating class. This way we can explicitly implement the comparison behavior between the objects.
public class Rating{ ... public override bool Equals(object obj) { var other = obj as Rating; if (other == null) return false; return this.Description.Equals(other.Description) && this.Id.Equals(other.Id); } public override int GetHashCode() { return this.Id.GetHashCode(); }}
public override bool Equals(object obj) { var other = obj as Rating;
if (other == null) return false;
return this.Description.Equals(other.Description) && this.Id.Equals(other.Id); }
public override int GetHashCode() { return this.Id.GetHashCode(); }}
If you have any further questions, please let me know.
Hello Tacho,
this will not work, because ComboItems is defined too late. In this small sample I could change the order but in my application I asynchronously load the combo items and the observable collection. Even if I implement IEquatable the Editor could not compare the instances. Do you have any other ideas how I can solve this Problem?
Thank you for the modified sample you have provided.
The reason for the combo editor's items to appear as object names instead of as their respective DisplayMemberPath values is due to the fact that the XamComboEditor cannot compare different Rating instances.In order to visualize the respective Item's values for the Rating property, you can include a reference to an object from the ComboItems collection inside the constructor of every ItemMVVM instance in the ViewModel.
Items = new ObservableCollection<ItemMVVM>(){ //new ItemMVVM("Laptop", 2000.0, new Model.Rating(3, "description 3")), //new ItemMVVM("TV", 800.0, new Model.Rating(6, "description 6")), //new ItemMVVM("DVD", 350.0, new Model.Rating(9, "description 9")), //new ItemMVVM("Smartphone", 930.0, new Model.Rating(7, "description 7")) new ItemMVVM("Laptop", 2000.0, ComboItems[2]), new ItemMVVM("TV", 800.0, ComboItems[4]), new ItemMVVM("DVD", 350.0, ComboItems[9]), new ItemMVVM("Smartphone", 930.0, ComboItems[0])};
new ItemMVVM("Laptop", 2000.0, ComboItems[2]), new ItemMVVM("TV", 800.0, ComboItems[4]), new ItemMVVM("DVD", 350.0, ComboItems[9]), new ItemMVVM("Smartphone", 930.0, ComboItems[0])};
When the items of the combo editor are of a primitive type like "int", the editor can compare and recognize the Item's rating and the dropdown's rating. This way it automatically selects the respective value.When the items of the combo editor are of a custom reference type like the Rating class, the editor cannot compare and recognize the different instances (due to the different places in the memory), even if their property values are the same. (for example when one is created for the ComboItems collection and one is created for the respective ItemMVVM).