c# - WPF mvvm 绑定(bind),获取/设置值 : issue with preventing update value

标签 c# wpf xaml mvvm setvalue

我在 WPF mvvm 环境中工作。

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

一个与其他不同:它是我的 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

最佳答案

Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => SelectedDocumentIndex= p_SelectedDocumentIndex ), DispatcherPriority.Send);

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

关于c# - WPF mvvm 绑定(bind),获取/设置值 : issue with preventing update value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156410/

相关文章:

c# - LiveCharts 基础知识——如何画线?

c# - 数据网格分组带来更好的性能

c# - 在不选择日期的情况下从日历控件获取值

c# - Windows 8 需要创建向导自定义控件

c# - 从数据读取器填充对象

c# - Avalonedit 如何使线路变压器失效

javascript - 如何将动态值传递给字节数组

c# - C# 中的冗余、可靠性和容错性 - 在哪里可以找到示例?

c# - 在 XAML 中绑定(bind)多边形点

c# - WPF 文本框 : set cursor position to last character received on property changed handler in View Model