Question

I'm starting to use MVVM,but I've been confused about something,there's my problem,I wanna add just one row in my table,and here's the way that i do this:

Viewmodel class:

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

Then:

 public ICommand SaveCommand
 {
     get { return new BaseCommand(Save); }
 }
 void Save()
 {
     if (DefaultSpecItem != null)
     {
        context.SaveChanges();
     }
 }

and i bind the datatatemplate to MyClass,it works prefectly and savechanges to my database,but don't update my view,in this case i want to return the id,so i put a textbox and bind it to id(prpoerty), what's the problem?am i missing something? i would appretiate any helps.

Was it helpful?

Solution

You have to implement INotifyPropertyChanged in order to make the binding work. Usually this implementation is moved in to the view model which wraps the model's properties and adds the change notification to it. However, there is nothing wrong doing it in the model directly. In this case you normally will make the model directly accessible in the view model via a property and use dot notation for binging (i.e. VM.Model.Property).

Personally, I prefer wrapping the properties as it allows greater flexibility and also makes the binding more understandable.

So here is an Example based on your model:

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);
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top