【问题标题】:WPF mvvm binding, get/set value: issue with preventing update valueWPF mvvm 绑定,获取/设置值:阻止更新值的问题
【发布时间】:2014-10-02 07:49:22
【问题描述】:

我在 WPF mvvm 环境中工作。

我有一些从 cs 文件到 xaml 的绑定变量和数据。

一个与其他不同:它是我的 tabsCollection 中所选标签的索引。当用户打开了多个标签并保存了模组时,我会向他显示一个对话框。如果他单击“确定”,则继续更改选项卡,如果单击“取消”,则选项卡必须保持不变。

这是我的代码:

private int p_SelectedDocumentIndex;
public int SelectedDocumentIndex{ get { return p_SelectedDocumentIndex; }
    set {
        if (tabsCollection.Count() > 1 && CanSave() == true)
        {
            if (dm.ShowMessage1(ServiceContainer.GetService<DevExpress.Mvvm.IDialogService>("confirmYesNo")))
            {
                p_SelectedDocumentIndex = value;
                base.RaisePropertiesChanged("SelectedDocumentIndex");
            }
            //else {
            //    CODE FOR NOT CHANGE THE VALUE 
            //}
        }
        else {
            p_SelectedDocumentIndex = value;
            base.RaisePropertiesChanged("SelectedDocumentIndex");
        }
    }
 }

所以,问题是:我如何才能不应用“设置”部分中的更改? (我认为就像撤消一样)

这是最简单的方法,但是,如果这种方法不正确,我该怎么办?

之前的失败尝试:

1)
p_SelectedDocumentIndex = p_SelectedDocumentIndex
base.RaisePropertiesChanged("SelectedDocumentIndex");

2)
base.RaisePropertiesChanged("SelectedDocumentIndex");

3)
nothing in the else branch

【问题讨论】:

  • 以下返回什么? dm.ShowMessage1(ServiceContainer.GetService("confirmYesNo"))
  • 我在问题中说过...它是一个索引。当前活动选项卡的索引。当我更改它时,我会向用户显示一个对话框。如果他点击“取消”,我不想改变值,所以我需要删除新的值并保留以前的。
  • 我认为 ShowMessage 通常会返回一个 dialogResult,您必须检查 DialogResult 是否正常或取消,然后根据 dialogresult,您将设置您的私有变量。否则,您不会设置它以保留“旧值”
  • 看看这对你有没有帮助 - joshsmithonwpf.wordpress.com/2009/09/04/….
  • @Krishna 对话框是由改变值触发的,不像你说的,所以我有这个问题

标签: c# wpf xaml mvvm setvalue


【解决方案1】:
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => SelectedDocumentIndex= p_SelectedDocumentIndex ), DispatcherPriority.Send);

此调用安排将 UI 状态恢复到操作开始之前的状态

【讨论】:

    【解决方案2】:

    我解决了。我从这里获取了解决方案:

    http://blog.alner.net/archive/2010/04/25/cancelling-selection-change-in-a-bound-wpf-combo-box.aspx

        public int SelectedDocumentIndex{ get { return p_SelectedDocumentIndex; }
            set {
                // Store the current value so that we can 
                // change it back if needed.
                var origValue = p_SelectedDocumentIndex;
    
                // If the value hasn't changed, don't do anything.
                if (value == p_SelectedDocumentIndex)
                    return;
    
                // Note that we actually change the value for now.
                // This is necessary because WPF seems to query the 
                //  value after the change. The combo box
                // likes to know that the value did change.
                p_SelectedDocumentIndex = value;
                if (tabsCollection.Count() > 1 && CanSave() == true)
                {
                    if (!dm.ShowMessage1(ServiceContainer.GetService<DevExpress.Mvvm.IDialogService>("confirmYesNo")))
                    {
                        Debug.WriteLine("Selection Cancelled.");
    
                        // change the value back, but do so after the 
                        // UI has finished it's current context operation.
                        Application.Current.Dispatcher.BeginInvoke(
                                new Action(() =>
                                {
                                    Debug.WriteLine("Dispatcher BeginInvoke " + "Setting CurrentPersonCancellable.");
    
                                    // Do this against the underlying value so 
                                    //  that we don't invoke the cancellation question again.
                                    p_SelectedDocumentIndex = origValue;
                                    DocumentPanel p = tabsCollection.ElementAt(p_SelectedDocumentIndex);
                                    p.IsActive = true;
                                    base.RaisePropertiesChanged("SelectedDocumentIndex");
                                }),
                                System.Windows.Threading.DispatcherPriority.ContextIdle,
                                null
                            );
    
                        // Exit early. 
                        return;
                    }
                }
                // Normal path. Selection applied. 
                // Raise PropertyChanged on the field.
                Debug.WriteLine("Selection applied.");
                base.RaisePropertiesChanged("SelectedDocumentIndex");
            }
        }
    

    【讨论】:

      【解决方案3】:

      如果您的 VM 类派生自 DependencyObject,那么您可以使用强制回调将属性更改为 DependecyProperty,从而启用“撤消”,如下所示:

          public int SelectedDocumentIndex
          {
              get { return (int)GetValue(SelectedDocumentIndexProperty); }
              set { SetValue(SelectedDocumentIndexProperty, value); }
          }
          public static readonly DependencyProperty SelectedDocumentIndexProperty =
              DependencyProperty.Register("SelectedDocumentIndex", typeof(int), typeof(MyViewModel), new PropertyMetadata(0,
                  (d, e) =>
                  {
                      //Callback after value is changed
                      var vm = (MyViewModel)d;
                      var val = (int)e.NewValue;
                  }, (d, v) =>
                  {
                      //Coerce before value is changed
                      var vm = (MyViewModel)d;
                      var val = (int)v;
                      if (vm.tabsCollection.Count() > 1 && vm.CanSave() == true)
                      {
                          if (vm.dm.ShowMessage1(ServiceContainer.GetService<DevExpress.Mvvm.IDialogService>("confirmYesNo")))
                          {
                              //no coerce is needed
                              return v;
                          }
                          else
                          {
                              //should coerce to the previous value
                              return VM.SelectedDocumentIndex;
                          }
                      }
                      else
                      {
                          //no coerce is needed
                          return v;
                      }
                  }));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        相关资源
        最近更新 更多