0
votes

I cannot for the life of me get my ViewModel to update my View when I replace a member with a new instance.

My ViewModel contains the proper (I believe) notification that the Test member has been changed through OnPropertyChanged (verified to work with binding to other members):

public class ServerViewModel
{
   private TestViewModel _Test;
   public TestViewModel Test 
   {
      get {return _Test;}
      set {_Test = value;OnPropertyChange("Test");}
   }
   ...
   ...
}

In my codebehind I instantiate the ServerViewModel in the MainWindow() constructor and set the datacontext:

VServer = new ServerViewModel();
this.DataContext = VServer;

In my view model I have a function that loads a test by assigning a new instance:

VServer.Test = new Test("c:\test\test3.test");

My XAML tries to bind to members of Test through:

<TextBox x:Name="TextTestName" 
...
Text="{Binding Test.Name, Mode=TwoWay}"/>

But when I load a test the control bindings do not recognize that a new object has been created and do not update their content. Any ideas?

1

1 Answers

1
votes

Could be a bunch of things - the code you've posted looks like pseudo code so it's difficult to determine.

First off, are you definitely implementing INotifyPropertyChanged on the ServerViewModel class?

Second of all, are you placing the viewmodel instantiation code in the constructor after the this.InitializeComponent();? (if not, you should be)

Thirdly, have you tested that Name is actually populated with a value when the 'Test' class is created? It should also be implementing the INotifyPropertyChanged interface.