Search code examples
c#wpftextblock

WPF TextBlock not updating


I have a standard texblock bound to a property in my viewmodel

  <TextBlock  Grid.Row="3"  Grid.Column="1" Text="{Binding MyErrorMessage, Mode=Default,UpdateSourceTrigger=PropertyChanged}"  Foreground="Red"></TextBlock>

The property

private string _errorMessage;
        public string MyErrorMessage
        {
            get { return _errorMessage; }
            set
            {
                _errorMessage = value;
                this.RaisePropertyChanged(this.MyErrorMessage);

            }
        }

I do a standard

 this.MyErrorMessage = "Login failed";

But the textblock is not updating. I can see the the setter and getter being called correctly, but still the textblock is not updating. Am i missing something fundamental?


Solution

  • The property that is raised should have the string "MyErrorMessage" and NOT the value of the property. i.e.

    this.RaizePropertyChanged( "MyErrorMessage" )
    

    If you fix this (and everything else is also set correct), you'll be fine.

    Side comments: There is no need for Mode=Default (as the name suggest, it is the default), and UpdateSourceTrigger=PropertyChanged (also the default, and doesn't make the code more readable).