开发者

WPF DataGrid Cancel Selection Change

开发者 https://www.devze.com 2023-03-23 00:38 出处:网络
I am working with WPF DataGrid in the MVVM manner and having trouble reverting the selection change from the ViewModel.

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 investi开发者_Go百科ng 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);
        }
    }
}


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 :)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号