Hello everybody,
in my WPF MVVM application I have a XamComboEditor, whose items and selected item are bound to ListOfItems and SelectedItem properties of my ViewModel, respectively.
<igWPF:XamComboEditor ItemsSource="{Binding ListOfItems}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedItem}" MinWidth ="100"/>
When an item is selected from the combo, some controls are shown, and the user can change the property values bound to them. What I would to achieve is STEP 5
How can I achieve this?
Thanks in advance
Valentina
Hello Valentina,
Thank you for the code-snippet and the description you have provided in regards to the selection logic you are looking for.
When MVVM is used, in order to prompt the user with a message for whether or not the current item's changes should be kept or discarded when changing the selected item, I can suggest you use a Behavior by handling the SelectedItemChanged event of the XamComboEditor. This would also require having an additional property that keeps the old value of the property that was changed. This will allow us to revert to it whenever the user decides to discard the changes.
ViewModel.Item:
public class Item{ public string Name { get; set; } public string OldName { get { return oldName; } set { // Set OldName only if it is not currently set or it should be reset. // This way only its first set is kept prior to the reset. if (oldName == null || (oldName != null && value == null)) oldName = value; NotifyPropertyChanged("OldName"); } }}
Behavior event:
private void AssociatedObject_SelectedItemChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<object> e){ var previousItem = e.OldValue as Item; if (previousItem == null) return; if (previousItem.OldName != null) { if (MessageBox.Show("The 'Name' of '" + previousItem.OldName + "' Item was modified. Do you accept the changes?", "Changes", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { previousItem.OldName = null; } else { previousItem.Name = previousItem.OldName; previousItem.OldName = null; AssociatedObject.SelectedItem = e.OldValue; } }}
if (previousItem.OldName != null) { if (MessageBox.Show("The 'Name' of '" + previousItem.OldName + "' Item was modified. Do you accept the changes?", "Changes", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { previousItem.OldName = null; } else { previousItem.Name = previousItem.OldName; previousItem.OldName = null; AssociatedObject.SelectedItem = e.OldValue; } }}
View:
<igEditors:XamComboEditor x:Name="combo" ... > <i:Interaction.Behaviors> <behaviors:XamComboEditorBehavior /> </i:Interaction.Behaviors></igEditors:XamComboEditor>
I have attached a sample application that demonstrates the approach from above.
If you have any questions, please let me know.
Tacho, your feedback has been helpful, valuable and precious!
Thanks a lot for your help, that's exactly what I needed
Thank you for the 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.