简体   繁体   中英

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?

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

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).

RaisePropertyChanged takes a string http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx

So just change the call to

this.RaisePropertyChanged("MyErrorMessage");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM