如何在MVVM视图中显示数据更改?

如何在MVVM视图中显示数据更改?,mvvm,mvvm-light,mvvm-toolkit,Mvvm,Mvvm Light,Mvvm Toolkit,我开始使用MVVM,但我对一些事情感到困惑,这是我的问题,我只想在我的表中添加一行,下面是我的方法: Viewmodel类: // Model.MyClass is my entity Model.MyClass myClass; public Model.MyClass MyClass { get { return myClass; } set {

我开始使用MVVM,但我对一些事情感到困惑,这是我的问题,我只想在我的表中添加一行,下面是我的方法:

Viewmodel类:

   // Model.MyClass is my entity
    Model.MyClass myClass;
    public Model.MyClass  MyClass
    {
        get
        {
            return myClass;
        }
        set
        {
            myClass= value;
            base.OnPropertyChanged(() => MyClass);
        }
    }

     context = new myEntities();
     myclass=new Model.MyClass();
     context.MyClass.AddObject(MyClass); 
然后:

我将datatatemplate绑定到MyClass,它可以很好地工作并将更改保存到我的数据库中,但不更新我的视图,在这种情况下,我想返回id,所以我放置了一个文本框并将其绑定到id(prpoerty), 有什么问题吗?我错过什么了吗?
我非常感谢您的帮助。

您必须实现
INotifyPropertyChanged
,才能使绑定正常工作。通常,此实现会移入视图模型,该视图模型包装模型的属性并向其添加更改通知。但是,直接在模型中执行此操作没有任何错误。在这种情况下,您通常会通过属性在视图模型中直接访问模型,并使用点符号进行Bing(即
VM.model.property

就个人而言,我更喜欢包装属性,因为它允许更大的灵活性,也使绑定更容易理解

下面是一个基于您的模型的示例:

public class ModelViewModel : ViewModelBase {
    public ModelViewModel() { 
        // Obtain model from service depending on whether in design mode
        // or runtime mode use this.IsDesignTime to detemine which data to load.
        // For sample just create a new model
        this._currentData = Model.MyClass();
    }

    private Model.MyClass _currentData;

    public static string FirstPropertyName = "FirstProperty";

    public string FirstProperty {
        get { return _currentData.FirstProperty; }
        set {
            if (_currentData.FirstProperty != value) {
                _currentData.FirstProperty = value;
                RaisePropertyChanged(FirstPropertyName);
            }
        }
    }

    // add additional model properties here

    // add additional view model properties - i.e. properties needed by the 
    // view, but not reflected in the underlying model - here, e.g.

    public string IsButtonEnabledPropertyName = "IsButtonEnabled";

    private bool _isButtonEnabled = true;

    public bool IsButtonEnabled {
        get { return _isButtonEnabled; }
        set {
            if (_isButtonEnabled != value) {
                _isButtonEnabled = value;
                RaisePropertyChanged(IsButtonEnabledPropertyName);
            }
        }
    }
}
当我更改Model.MyClass(实体模型类)并从INotifyPropertyChanged继承它时,如下所示:int-id;公共虚拟int-Id{get{return Id;}set{Id=value;PropertyChanged(这是newPropertyChangedEventArgs(“Id”);}}它可以工作,但我不确定是否应该从INotifyPropertyChanged继承模型的类?
public class ModelViewModel : ViewModelBase {
    public ModelViewModel() { 
        // Obtain model from service depending on whether in design mode
        // or runtime mode use this.IsDesignTime to detemine which data to load.
        // For sample just create a new model
        this._currentData = Model.MyClass();
    }

    private Model.MyClass _currentData;

    public static string FirstPropertyName = "FirstProperty";

    public string FirstProperty {
        get { return _currentData.FirstProperty; }
        set {
            if (_currentData.FirstProperty != value) {
                _currentData.FirstProperty = value;
                RaisePropertyChanged(FirstPropertyName);
            }
        }
    }

    // add additional model properties here

    // add additional view model properties - i.e. properties needed by the 
    // view, but not reflected in the underlying model - here, e.g.

    public string IsButtonEnabledPropertyName = "IsButtonEnabled";

    private bool _isButtonEnabled = true;

    public bool IsButtonEnabled {
        get { return _isButtonEnabled; }
        set {
            if (_isButtonEnabled != value) {
                _isButtonEnabled = value;
                RaisePropertyChanged(IsButtonEnabledPropertyName);
            }
        }
    }
}