WPF bindings do not pick up any changes

148 views Asked by At

In my WPF application, I have some properties which I have bound to the XAML counterpart, but for some reason do not get set whenever their values change. I have implemented the INotifyPropertyChanged interface as well as set my DataContext for this View too, and it is still not picking up any changes.

I have this same pattern for other properties within this ViewModel which do work, while others don't.

Here is a snippet of my current code:

ViewModel

public class TestViewModel : INotifyPropertyChanged
{
    private string testString;

    public TestViewModel()
    {
        .....
        this.RunCommand = new RelayCommand(this.RunAction);
    }

    public string TestString
    {
        get
        {
            return this.testString;
        }

        set
        {
            this.testString = value;
            this.OnPropertyChanged("TestString");
        }
    }

    private void RunAction()
    {
        .....
        this.testString = "Running.";
    }
}

View

<StatusBarItem>
    <TextBlock Text="{Binding Path=TestString, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
</StatusBarItem>

DataContext (set in code-behind of another MainWindow class)

var testViewModel = SimpleIoc.Default.GetInstance<TestViewModel>();
var testWindow = new TestWindow() { DataContext = testViewModel };
testingWindow.Show();

If it helps, this is part of a multi-windowed application which uses MVVM-Light to pass properties between classes.

2

There are 2 answers

2
Janne Matikainen On BEST ANSWER

You are not changing the value of the TestString, you are assigning a command to change the value but you do not seem to be executing it.

this.RunCommand = new RelayCommand(this.RunAction);

Bind that command to something or execute it manually from somewhere.

Also you need to assign the property not the field

this.TestString = "Running.";
2
Gertjan Gielen On

I found the problem. You are only updating the private property testString. But you do not update the property TestString so the notify is never called.

Try this:

this.TestString = "Running";