WEFX
WEFX

Reputation: 8552

Update a WPF label using Binding

On my wpf application, I’m trying to update a label whenever a certain variable changes in my ViewModel. Do you know why the label won’t update?

Here’s what I have at the moment:

xaml

<Label Name="myLabel" Content="{Binding myState}" />

myViewModel.cs

in the constructor:

_MyObject.myEvent += myNewEvent;

and later...

private void myNewEvent(object sender, myArgs e)
{
    myState = someStringVariable;
}

Upvotes: 0

Views: 336

Answers (2)

Blachshma
Blachshma

Reputation: 17385

My guess is that your ViewModel doesn't implement INotifyPropertyChanged.
If you want the UI to reflect changes of properties in the ViewModel, the Viewmodel must implement INotifyPropertyChanged and raise a PropertyChanged event in the property's setter whenever the value is changed.

For more information: How to: Implement the INotifyPropertyChanged Interface

Upvotes: 1

Tieson T.
Tieson T.

Reputation: 21191

Likely, you need to set set the DataContext of the element that contains this label to the instance of your type which I assume _MyObject represents. If you've done that already, it should update whenever you change the property value.

Upvotes: 1

Related Questions