GodsCrimeScene
GodsCrimeScene

Reputation: 1270

Model changes updating View-Model WPF

I am having an issue getting my model changes updated back into my viewmodel so i can display. In this example i have a label and a button, when i press the button it will execute some business logic, and should update the label on screen. However, when my model changes the view will not. Any Idea on what i am doing wrong here?

View-

<Window.DataContext>
    <vm:ViewModel>
</Window.DataContext>
<Grid>
    <Label Content="{Binding Path=Name}"/>
    <Button Command={Binding UpdateBtnPressed}/>
</Grid>

ViewModel

public ViewModel()
{
    _Model = new Model();
}

public string Name
{
    get{return _Model.Name;}
    set
    {
        _Model.Name = value;
        OnPropertyChanged("Name");
    }
}

public ICommand UpdateBtnPressed
{
get{
_UpdateBtn = new RelayCommand(param => UpdateLabelValue());
return _UpdateBtn;
}

private void UpdateLabelValue()
{
    _Model.Name = "Value Updated";
}

Model

private string name = "unmodified string";

public string Name
{
    get{return name;}
    set{name = value;}
}

Upvotes: 1

Views: 4064

Answers (3)

Baris
Baris

Reputation: 455

Your model must implement INotifyPropertyChanged such as;

public class Personel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; OnChanged("Name");}
        }


        void OnChanged(string pn)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(pn));
            }
        }
    }

Upvotes: 1

Andre
Andre

Reputation: 1238

It seems you've missed to implement the INotifyPropertyChanged interface.

Upvotes: 1

Memoizer
Memoizer

Reputation: 2289

Try this:

private void UpdateLabelValue()
{
  Name = "Value Updated";
}

Upvotes: 6

Related Questions