Hello everybody,
I have a MVVM application. In My ViewModel, I have a MAPPINGS property, which is a Dictionary <string,string>. I would like to map this property to a XamDataGrid: first column will represent Key, second column will represent the value. So far so good, I can do this
What I need help for is:
Value (second column) can be changed by the user, and can be only [Value1, Value2, Value3] (from "Available_Mappings" property in my ViewModel)
So, when user click on Value columns, I would need a combo displaying allowed values. I am having troubles configuring the ComboBoxField
How can I do that?
thanks in advance for your help
Valentina
Hello Valentina,
Thank you for your feedback.
I am glad to know that I was able to help you achieve the desired functionality. I believe this thread can help other people looking for a similar solution.
If you have any questions, please let me know.
Hello Tacho,
thanks your example was perfect as usual, it helped me a lot solving my issue.
Thanks again
valentina
Thank you for the application details you have provided.
The Dictionary<TKey, TValue> works with KeyValuePair<Tkey, TValue> struct instances. Since the KeyValuePair is immutable (the Key and Value properties do not have setters), we cannot edit it through the XamDataGrid. This is why we will not be able to change the Value of the DataItem by using the ComboBoxField.
An approach I can suggest you in order to preserve the mappings functionality and be able to change the Value is to create a custom KeyValuePair class that has setters for its properties.This way we can populate an ObservableCollection with the custom KeyValuePair instances and bind it to the XamDataGrid's DataSource.
public class CustomKeyValuePair<TKey, TValue>{ public TKey Key { get; set; } public TValue Value { get; set; } ...}
<igDP:FieldLayout.Fields> <igDP:TextField Name="Key" AllowEdit="False" /> <igDP:ComboBoxField Name="Value"> <igDP:ComboBoxField.EditorStyle> <Style TargetType="igEditors:XamComboEditor"> <Setter Property="ItemsSource" Value="{Binding Path=DataContext.Values, RelativeSource={RelativeSource AncestorType=igDP:XamDataGrid}}" /> </Style> </igDP:ComboBoxField.EditorStyle> </igDP:ComboBoxField></igDP:FieldLayout.Fields>
Another approach I can suggest you is to implement a custom Dictionary and KeyValuePair, which will allow you to operate with mutable underlying instances (depending on the functionality you are looking for).
I have attached a sample application that demonstrates the initial approach from above.