【问题标题】:WPF DataGrid Cancel Selection ChangeWPF DataGrid 取消选择更改
【发布时间】:2011-10-14 04:43:36
【问题描述】:

我正在以 MVVM 方式使用 WPF DataGrid,但无法从 ViewModel 还原选择更改。

有没有经过验证的方法可以做到这一点?我最新试用的代码如下。现在我什至不介意在背后的代码中进行黑客攻击。

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);
        }
    }
}

【问题讨论】:

    标签: wpf data-binding wpfdatagrid selectionchanged


    【解决方案1】:

    经过几天的反复试验,终于让它工作了。以下是代码:

        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);
        }
    

    这里的关键是使用来自数据绑定网格集合(即:SearchResults)的全新的原始选定项,而不是使用选定项的副本。看起来很明显,但我花了几天时间才弄清楚!感谢所有帮助过的人:)

    【讨论】:

      【解决方案2】:

      如果您想阻止选择更改,可以尝试this

      如果你想恢复选择,你可以使用 ICollectionView.MoveCurrentTo() 方法(至少你必须知道你想选择什么项目;))。

      我不太清楚你真正想要什么。

      【讨论】:

      • 您好,blinkmeris,抱歉回复晚了。我试图做的是向用户显示一条确认消息以在用户选择不同的结果时保存更改(在 DispatchSelectionChange 内),如果用户选择取消它,我需要将选择恢复为原始选择。
      • 我尝试了您指出的两种方法。似乎没有一个对我有用:(在我的情况下,键盘和鼠标事件点击不合适。而且 ICollectionView 的行为不应该是这样。情况是,即使 CurrentItem 已同步,网格在视觉上显示另一个元素为已选中。在我看来是 WPF 中的一个错误...
      猜你喜欢
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2013-03-31
      相关资源
      最近更新 更多