Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1280
XamComboEditor cancel event
posted

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

  1. Select an item from the combo box
  2. Change some properties
  3. Select an other item from the combo box
  4. a message box is shown to the user, asking to save or discard data, or to cancel the operation. 
  5. If the user chooses to cancel the operation, SelectedItem must rollback to its previous value, i.e. I need to cancel the event.

How can I achieve this?

Thanks in advance

Valentina

Parents
  • 6365
    Verified Answer
    Offline posted

    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;
            }
        }
    }

    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.

    XamComboEditor_sample.zip
Reply Children