Why does not the view gets updated in binding?

24 views Asked by At

I am fairly new to MVVM and wpf. I was implementing a simple login page that connects to the database to retrieve user details. In the view model i have a string field called Error that stores any error like wrong password or connection error to show up on view. I bound the error to a textblock in view. So on pressing the login button if some error happens i update the text of error. But the problem is the change is not reflected in view.

string _error;
public string Error
    {
        get { return _error; }
        set { _error = value; }
    }

update the field as

_error = "Wrong password!";

In the view:

<TextBlock Text="{Binding Error, UpdateSourceTrigger=PropertyChanged}"/>

What is the problem?

Edit: I read article on Code Project and am implementing it in similar way. Still it is not working?

2

There are 2 answers

2
Nitram On

You need to send out a property changed event from the ViewModel. The class needs to implement the INotifyPropertyChanged interface that contains the event you need to fire. The parameter of the event needs to be the name of the property you changed.

0
Contango On

Add the interface INotifyPropertyChanged to your ViewModel.

Accept the recommendation for ReSharper to implement the interface for you (you might have to install ReSharper).

In the setter for the property, add OnPropertyChanged("Error");.

Now, if you useError ="test";` it will run the setter, which runs the property notify changed, which pushes the change into the View so it can be seen.