Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Why does VM not get notified when Property in Model is changed?

$
0
0

Hi all,

I am quite new to WPF, MVVM and all that stuff and I got stuck at validating my data.

I have the following Model, ViewModel und xaml-Snippet, with Validation in the Model:

public class Model
{
    private string _someString;
    public string SomeString 
    {
        get{ return _someString;}
        set
        { 
         _someString = value;}
    }
    private bool _isValid;
    public bool IsValid
    {
        get {return _isValid;}
        set {_isValid = value;}
    }
    public string Validate(string propertyName)
    {
        switch(propertyName):
            case "SomeString":
                if(SomeString.Length < 4)
                {
                    IsValid = false;
                    return "SomeString too short!";
                }
    }
//...
}
public class ViewModel : IDataErrorInfo, INotifyPropertyChanged
{
    public string SomeString
    {
        get { return _model.SomeString;}
        set 
        { 
            _model.SomeString = value; 
            NotifyPropertyChanged("IsValid");
        }
    }
    public bool IsValid
    {
        get { return _model.IsValid; }
        set {_model.IsValid =  value; }
    }
    public string this[string propertyName]
    {
        get {return _model.Validate(propertyName);}
    }
}<TextBox Text="{Binding SomeString, Mode=TwoWay, ValidatesOnDataErrors = True}"/><Button Content="Save" IsEnabled="{Binding IsValid}"/>

I already tried diffrent variations:
1. Notification in the MODEL => No Notification to the VIEWMODEL
2. Notification in the ViewModel-Setter of SomeString (PropertyChanged("IsValid")) => IsValid is Notified, than Validation runs. Thus IsValid changes but VM still is not notified...

Am I getting something wrong? Due to a lot of threads it should work like that:
Model Prop Changes --> Notifies VM via INotifyPropertyChanged --> Notifies View

I hope I could describe my problem in a suitable way.

Thanks Kat


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>