使用MVVM取消wpf中的组合框中的选择 [英] Cancelling selection in combobox in wpf using MVVM

查看:259
本文介绍了使用MVVM取消wpf中的组合框中的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List类型的组合框。我有ItemsSource和ItemSelected绑定通过数据报文。如果选定的项目已更改,那么我会显示一个确认用户操作的弹出消息。单击确定时,选择会更改。但是在点击取消时,应该取消选择,并保留前一个项目。以下是绑定到组合框的SelectedItem的属性。

I have a combobox of type List. I have the ItemsSource and the ItemSelected bound through the datacontext. If the selected item has been changed then I show a pop up message confirming the users action. On clicking of 'Ok' the selection gets changed. But on clicking of cancel, the selection should be cancelled and previous item should be retained. Below is the property that is bound to SelectedItem of the combobox.

Public SomeClass Sel
{
  get
  {
    return _sel;
  }
  set
  {
    if (_sel != value)
    {
      var sview = _sel;

      if (Compare())
      {
        _sel = value;

        if (Sel != null)
          IsDefault = Sel.IsDefault;
        OnPropertyChanged(() => Sel);
      }
      else
      {
        MessageBoxResult result = MessageBox.Show("Message.", "Owb Message", MessageBoxButton.OKCancel);
        if (result == MessageBoxResult.OK)
        {
          _sel = value;
          if (Sel != null)
            IsDefault = Sel.IsDefault;
          OnPropertyChanged(() => Sel);
        }
        else
        {
          Application.Current.Dispatcher.BeginInvoke(new Action(() =>
          {
            _sel = sview;
            OnPropertyChanged("Sel");
          }), DispatcherPriority.Send, null);
          return;
        }
      }
    }
  }
}


$ b b

组合框位于弹出窗口中。那么Dispatcher对象在这种情况下是否工作?

The combo box is in a pop window. So would Dispatcher object work in that case?

推荐答案

我错过了在调用dispatcher之前设置的值。
_sel = sview

It works like magic now! I missed out setting the value before calling dispatcher. _sel = sview

这篇关于使用MVVM取消wpf中的组合框中的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆