3

I am working with WPF DataGrid in the MVVM manner and having trouble reverting the selection change from the ViewModel.

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.

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(() => NotifyOfPropertyChange(() => SelectedSearchResult)));
                return;
            }
            NotifyOfPropertyChange(() => SelectedSearchResult);
        }
    }
}
Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63

2 Answers2

3

After days of trial and error, finally got it working. Following is the code:

    public ActorSearchResultDto SelectedSearchResult
    {
        get { return _selectedSearchResult; }
        set
        {
            if (value != _selectedSearchResult)
            {
                var originalSelectionId = _selectedSearchResult != null ? _selectedSearchResult.Id : 0;
                _selectedSearchResult = value;
                if (!DispatchSelectionChange(value)) // Returns false if the selection has to be cancelled.
                {
                    // Invokes the property change asynchronously to revert the selection.
                    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ContextIdle, new Action(() => RevertSelection(originalSelectionId)));
                    return;
                }
                NotifyOfPropertyChange(() => SelectedSearchResult);
            }
        }
    }

    private void RevertSelection(int originalSelectionId)
    {
        _selectedSearchResult = SearchResults.FirstOrDefault(s => s.Id == originalSelectionId);
        NotifyOfPropertyChange(() => SelectedSearchResult);
    }

Key here is to use a brand new originally selected item from the databound grid's collection (ie: SearchResults) rather than using a copy of the selected item. It looks obvious, but took days for me to figure it out! Thanks for everyone who helped :)

Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63
1

if you want to prevent the selection change you can try this.

if you want to revert a selection, you can just use ICollectionView.MoveCurrentTo() methods (at least you must have to know what item you want to select ;)).

its not quite clear to me what you really want.

Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • hi blinkmeris, sorry for late replying. what im trying to do is showing a confirmation message to the user to save changes (inside DispatchSelectionChange) when he selects a different result and if the user opted to cancel it, i need to revert the selection to the original one. – Ε Г И І И О Aug 23 '11 at 06:08
  • I tried both methods you pointed out. None seem to work for me :( the keyboard and mouse event tapping is not suitable in my case. And the ICollectionView does not behave as it should be. The case is, even though the CurrentItem is synced, the grid visually shows another element as selected. Seem to me a bug in WPF... – Ε Г И І И О Aug 25 '11 at 05:34