title: WPF DataGrid Cancel Selection Change
body: <p>I am working with WPF DataGrid in the MVVM manner and having trouble reverting the selection change from the ViewModel.</p>
  
  <p>Is there any proven way to do this? My latest tried out code is below. Now I do not even mind investing on a hack inside the code behind.</p>
  
  <pre><code>public SearchResult SelectedSearchResult
  {
      get { return _selectedSearchResult; }
      set
      {
          if (value != _selectedSearchResult)
          {
              var originalValue = _selectedSearchResult != null ? _selectedSearchResult.Copy() : null;
              _selectedSearchResult = value;
              if (!DispatchSelectionChange(value)) // Returns false if the selection has to be cancelled.
              {
                  _selectedSearchResult = originalValue;
                  // Invokes the property change asynchronously to revert the selection.
                  Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() =&gt; NotifyOfPropertyChange(() =&gt; SelectedSearchResult)));
                  return;
              }
              NotifyOfPropertyChange(() =&gt; SelectedSearchResult);
          }
      }
  }
  </code></pre>