0

Issue: I have a list of a class that's being displayed in a Datagrid, one of the values needs to ask the user if they really want to change it. This was implemented with a YesNo MessageBox in the value's Setter.

The problem is that this messagebox should not come up every time the Setter is called, like when a new object is being added to the datagrid with a dialog, it will still ask if you are sure you want to change the value of what is currently being created.

I'm not sure if there's a clean way of doing this, so any help is appreciated.

Right now the setter in the class looks like this:

public string Value
{
  get { return _value; }
  set 
  {
       string message = "Are you sure you want to modify this value?";
       MessageBoxResult result = MessageBox.Show(message, "Confirmation",
       MessageBoxButton.YesNo, MessageBoxImage.Question);
       if (result == MessageBoxResult.Yes)
       {
          _value = value;
       }
       else
       {
        // Set to previously used value
        Value = _value;
       }
          RaisePropertyChanged("Value");
  }
}
Community
  • 1
  • 1
aitbg
  • 27
  • 3
  • 1
    I'd argue that is bad UX and you should consider an alternate way of nagging the user. Maybe make the column editable to users with a specific role, or only nag them once when saving all their edits? – slugster Jun 17 '19 at 23:35

1 Answers1

1

What you're trying to do is UI related. You may want to check this out. https://stackoverflow.com/a/45397612/7182460

The easiest way would be to use the CellEditEnding event.

private void Dg_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
        string message = "Are you sure you want to modify this value?";
        MessageBoxResult result = MessageBox.Show(message, "Confirmation",
        MessageBoxButton.YesNo, MessageBoxImage.Question);
        if (result != MessageBoxResult.Yes)
        {
            (sender as DataGrid).CancelEdit();
        }
    }
}
Neil B
  • 2,096
  • 1
  • 12
  • 23