0
votes

I'm very new to WPF and MVVM, and it's been causing me a lot of headaches. Due to issues with navigation, I decided to just have all my content visible at once. I thought I would create a new ViewModel (MainViewModel) to contain my two other ViewModels (StudentViewModel and AddStudentsViewModel).

MainViewModel contains something like this:

private StudentViewModel _studentVM;
private AddStudentsViewModel _addStudentsVM;

public StudentViewModel StudentVM
{
    get { return _studentVM; }
    set
    {
        if (_studentVM != value)
        {
            _studentVM = value;
            NotifyPropertyChanged("StudentVM");
        }
    }
}

(public AddStudentsViewModel AddStudentsVM exists as well, I'm just trying to keep this short)

I have successfully bound StudentVM and AddStudentsVM to my main View, as I can programmatically set values during the initialization phase and when debugging, I can see my button clicks are being redirected to the correct methods. It even seems like I am successfully adding students to objects, however my main View isn't reflecting these changes.

Am I missing something in MainViewModel? Or is it not possible for a ViewModel to see the changes in any other ViewModels inside it?

2
Your post will still be short if you add the part of the View code where binding is located.tgpdyk
You will need to expose a method on your child view models which allows you to raise NotifyPropertyChanged() on those. Then in the methods you can just use an empty string for the property name, which causes a refresh for all bindings in the view from that view model.Geoff James
You need to add some more information to your question. Which property are you calling the setter on? How is the view referencing the datacontext? What does the binding in the xaml look like? What you are expecting, and what are you actually getting? Provide this information and you will likely get more concrete answers.Phillip Ngan

2 Answers

0
votes

If your view is bound to some property inside StudentViewModel through nested navigation into the property StudentVM like StudentVM.Property, then to reflect changes on StudentVM.Property unto your view would require that you notify the view that StudentVM (not StudentVM.Property) has changed.

So it depends on how you defined your binding and on which property you're raising the PropertyChanged event.

-1
votes

A viewmodel which contains two other viewmodels. Just think about it for a second. It's not a good idea.

Anyway, you have to implement INotifyPropertyChanged in your containing viewmodels as well. Not only in the containing MainViewModel.

Maybe that is the fault?