0

I have a window with multiple textbox inputs. When I click Edit it loads in the current user and if i make changes then decide to cancel it doesn't remove the changes on cancel.

If I try to edit the same person again the changes I made previously are still in the input fields.

This is how I call the edit window

<Button x:Uid="Button_EditCommand" Margin="0,0,2,0" Command="{Binding Path=EditProviderCommand}" Style="{StaticResource btnCustom}" MinWidth="75" Content="Save" Visibility="{Binding Path=IsEditing, Converter={StaticResource VisibilityConverter}}" />

This then calls the box and this is my cancel method

CancelCommand = new RelayCommand(Cancel);
public ICommand CancelCommand { get; private set; }

private void Cancel()
  {
     Provider = null;
     OnRequestClose();
  }

The cancel method is shared with the Add method. When I type in the Add and click cancel it clears all changes. How do I get it to clear out the edited fields instead of binding them to the binding?

TextBoxes:

<TextBox x:Uid="TextBox_1" Grid.Column="1" Grid.Row="0" Tag="{Binding Path=FirstNameLabel, Source={StaticResource Clientization}}" Style="{StaticResource EditTextBox}" MaxLength="35"
                 Text="{Binding Path=Provider.FirstName, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox x:Uid="TextBox_2" Grid.Column="2" Grid.Row="0" Tag="Middle" MinWidth="75" Style="{StaticResource EditTextBox}" MaxLength="30"
                 Text="{Binding Path=Provider.MiddleName, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox x:Uid="TextBox_3" Grid.Column="1" Grid.Row="1" Tag="{Binding Path=LastNameLabel, Source={StaticResource Clientization}}" Style="{StaticResource EditTextBox}" MaxLength="60"
                 Text="{Binding Path=Provider.LastName, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox x:Uid="TextBox_4" Grid.Column="2" Grid.Row="1" Tag="Suffix" Style="{StaticResource EditTextBox}" MaxLength="20"
                 Text="{Binding Path=Provider.Suffix, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox x:Uid="TextBox_5" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Tag="List Name*" Style="{StaticResource EditTextBox}" MaxLength="160"
                 Text="{Binding Path=Provider.ListName, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox x:Uid="TextBox_6" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="3" Tag="NPI*" Style="{StaticResource EditTextBox}" MaxLength="80"
                 Visibility="{Binding Path=HideNpi, Source={StaticResource Clientization}, Converter={StaticResource TernaryConverter}, ConverterParameter='True ? Collapsed : Visible'}"
                 Text="{Binding Path=Provider.NPI, UpdateSourceTrigger=PropertyChanged}"/>

Update:

My copy of provider

public AddEditProviderDialogViewModel(IProviderRepository providerRepository, IMedListProvider medListProvider,
     Provider provider, bool isEditing)
     : this(providerRepository, medListProvider, provider)
  {
     _isEditing = isEditing;
     Provider editProvider = new Provider()
     {
        FirstName = provider.FirstName,
        LastName = provider.LastName,
        Prefix = provider.Prefix,
        Suffix = provider.Suffix,
        ListName = provider.ListName,
        NPI = provider.NPI,
        OrgName = provider.OrgName,
        Address1 = provider.Address1,
        Address2 = provider.Address2,
        City = provider.City,
        State = provider.State,
        Zip = provider.Zip,
        EmailAddress = provider.EmailAddress,
        Phone1Type = provider.Phone1Type,
        Phone2Type = provider.Phone2Type,
        Phone1 = provider.Phone1,
        Phone2 = provider.Phone2            
     };
TheDizzle
  • 1,534
  • 5
  • 33
  • 76
  • Do you use binding when displaying current user values in textboxes? Then you change source values as you leave focus (or as you type if `UpdateSourceTrigger=PropertyChanged`). This means you lose original data and you have to restore it. You can create a copy of original user data when you start edit and if you decide to cancel - discard copy of data somehow. – Sinatr Apr 30 '15 at 13:58
  • thats exactly what I need to do, and have no idea how to do it. – TheDizzle Apr 30 '15 at 14:28
  • Switching between edit/view mode can be done in ViewModel. Its properties (exposing underlying data, e.g. `Provider` or maybe `Provider.FirstName`) can instead get/set values from a copy of `Provider`. When you cancel edit - properties will again show real data. You can create copy of real data every time when edit mode is enabled. – Sinatr Apr 30 '15 at 14:39
  • I updated what i'm doing to copy the provider, but I'm unsure how to save it. – TheDizzle Apr 30 '15 at 17:54
  • Does this answer your question? [How to cancel an edit to an object using MVVM?](https://stackoverflow.com/questions/1091036/how-to-cancel-an-edit-to-an-object-using-mvvm) – StayOnTarget May 04 '20 at 18:47

3 Answers3

1

Presumably with Add you just do nothing with the edited model and next time you create a new one. I can't see anything would get cleared - just not added.

Cancel won't undo any changes, you'll have to handle that yourself. Perhaps make a clone of the current item and edit that instead. When accepted, you'll have to copy the changes back to the original.

If you need any specific help, we'll need to see some more code. A simple example, perhaps.

Charles Mager
  • 25,735
  • 2
  • 35
  • 45
0

The way i did it is: create a copy of the object. Then created an undo method and binded it to the cancel. The undo method would do something like this: editedobject = originalobject. Ill post code when i get home.

Cristian
  • 275
  • 4
  • 17
0

If you have a UserViewModel class that wraps a User model instance, you can implement cancelling an edit by using the sort of pattern below. Basically, on edit you copy your model to an _originalModel variable and then choose whether or not to revert to the original model on cancel.

public class UserViewModel : ViewModelBase
{
    private User _originalModel;
    private User _model;

    public User Model
    {
        get { return _model; }
        set
        {
            _model = value;
            _originalModel = _model;
        }
    }

    public DelegateCommand EditCommand { get; set; }
    public DelegateCommand CancelCommand { get; set; }
    public DelegateCommand SaveCommand { get; set; }

    public UserViewModel()
    {
        EditCommand = new DelegateCommand(OnEditCommandExecuted);
        CancelCommand = new DelegateCommand(OnCancelCommandExecuted);
        SaveCommand = new DelegateCommand(SaveCommandExecuted);
    }

    private void SaveCommandExecuted(object obj)
    {
        _originalModel = _model;
    }

    private void OnCancelCommandExecuted(object obj)
    {
        _model = _originalModel;
        OnPropertyChanged(null);
    }

    private void OnEditCommandExecuted(object obj)
    {
        _originalModel = _model;
    }

    public string FirstName
    {
        get { return Model.FirstName; }
        set
        {
            if (value == Model.FirstName) return;
            Model.FirstName = value;
            OnPropertyChanged();
        }
    }

    public string LastName
    {
        get { return Model.LastName; }
        set
        {
            if (value == Model.LastName) return;
            Model.LastName = value;
            OnPropertyChanged();
        }
    }
}
Barracoder
  • 3,696
  • 2
  • 28
  • 31