0

I have created a errorMessage as a string in my ViewModel. When you get an error or stuff like that, I will use this one to display a message to the user.

In ViewModel

I have implemented the INotifyPropertyChanged interface, also I have created this block:

#region INotifyPropertyChanged members
    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion

In top of the ViewModel, I have created a string called ErrorMessage.

public string ErrorMessage { get; set; }

In the constructor, I have tested it with

 ErrorMessage = "Error message";

And I can see the text in my view on load. The problem is when I hit an exception like in a try / catch block and sets the error to a text, it wont get updated.

In the Exception or on a button click, I have tried entering:

ErrorMessage = "No rooms with entered settings were found";

The View

<StackPanel Margin="10, 5" Grid.Row="5" Grid.ColumnSpan="3">
        <TextBlock FontFamily="../Fonts/bold.ttf#bold" Height="40" Foreground="Red" Text="{Binding ErrorMessage, UpdateSourceTrigger=PropertyChanged}"></TextBlock>
    </StackPanel>

If you scroll to the right, you can see that I have binded ErrorMessage and given it the UpdateSourceTrigger=PropertyChanged.

What do I need, so I can in my ViewModel, set the ErrorMessage equal to a text on certain scenarios?

3
  • Have you set the DataContext of your view to your ViewModel? Commented Apr 17, 2016 at 19:26
  • You need to call NotifyPropertyChanged in the setter of the property, you can't use an Auto property. Commented Apr 17, 2016 at 19:30
  • ChrisF, yes, else it wont have worked when I set the ErrorMessage in the constructor, its after when the context is set that I want to change errorMessage based on scenario Commented Apr 17, 2016 at 19:33

2 Answers 2

1

You must call NotifyPropertyChanged("ErrorMessage") in set method. Extend your property with full get and set accessors implementation (with a private variabile) and then call notifypropertychanged in the set method after privateproperty= value line.

It's important to Notify your view when a property changes.

S

Sign up to request clarification or add additional context in comments.

Comments

0

Change your ErrorMessage Property as:

private string errorMessage;

    public string ErrorMessage
    {
        get { return errorMessage; }
        set 
        { 
            errorMessage = value;
            NotifyPropertyChanged("ErrorMessage");
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.