WPF MVVM PropertyChangedEventHandler always null - Not updating Binding [Answered]
I have a MainWindow which loads a view (UserControl) in a ContentControl as Content per Binding to an Property in my MainViewModel.
The initial Load works fine but changes made afterwards don't get published to the MainView.
OnPropertyChanged is being called- and INotifyPropertyChanged is implemented via an "ObservableObject" Wrapper-Class.
The PropertyChangedEventHandler is always null the View never attaches to it.
My MainWindow
My ViewModel
21 Replies
RelayCommand Class
ObservableObject Class
By setting the
in code instead of xaml the EventHandler isn't null anymore. But the Content is still not updated even thou the Value of the Binded Property changed
in code instead of xaml the EventHandler isn't null anymore. But the Content is still not updated even thou the Value of the Binded Property changed
UpdateSourceTrigger
is wrong -- that's about when the ContentControl
writes back to the CurrentView
property (the "source"), which it should never do. I don't think that's your issue though
But the Content is still not updated even thou the Value of the Binded Property changedAre you sure the binding isn't updating? Note that you're binding a ViewModel, but the ContentControl only knows how to display UI elements (i.e. views), so it isn't going to show anything when you give it a ViewModel to display
Almost forgot, ViewModels and Views are linked via App.xaml DataTemplates
I can only see the Value change in the Property
Where's that?
Ah, you said App.xaml
I still stand by my question -- do you know whether the binding's getting updated? It might be the ViewModel->View stuff that's breaking
You can turn up the binding debug level, put a converter on it, etc
Also, https://github.com/canton7/Stylet 🙂
I thought i know but the issues i have gets me questioning myself right now.
I'm confused about the Fact that the ContentControl loads the StartVm Viewmodel as the View I've assigned in the App.xaml correctly.
Same happens when i use DummyVm as the initial Value in MainViewModel's Constructor.
Only after changing the Value the issue persists, which is why i can't wrap my head around it.
I can verify that the Value of the Binded Property Changed to the Correct Value; and the PropertyChanged event is being fired with "something" attached to it.
If the initial Load works with the viewmodels as the values for "CurrentView"; shouldn't the same work via an Command updating the Binding?
Thank you, I'll take a look later today
I'm confused about the Fact that the ContentControl loads the StartVm Viewmodel as the View I've assigned in the App.xaml correctly.Good point.
If the initial Load works with the viewmodels as the values for "CurrentView"; shouldn't the same work via an Command updating the Binding?Yeah, it should do Next step it to turn up the binding debug level I think Debug -> Options -> Debugging -> Output Window -> Data Binding, set to something high

(then check the Output window)
The Clicked event get's through and the Value is changed..
Except of the initial loading i don't get any logs about Bindings doing anything. LogLevel is set to All

Alternatively tried using the PresentationTraceSources.TraceLevel=High - Still the same no single Log message
The Changed Is being executed

Issue resolved..
I was just dumb trying to update a MainViewModel instance with another one..
Cool! What do you meian, "with another one"?
Sorry for the late reply,
The issue was in my StartView that has a DataContext set to MainViewModel..
I've ended up creating a "middle-man" class like that
The MainViewModel Subscribes like that
And the StartViewModel Executes a Command like that
Beforehand the StartView wrote to a different instance of MainViewModel instead of StartViewModel
This is getting quite painful, haha. I'm not sure I follow why StartView knows anything about MainViewModel at all
It doesn't anymore.. That was my mistake..
Because I've had a flaw in thoughts in how to design my MainViewModel.
Now that i correctly use MVVM everything works out fine!
A decent mvvm framework will remove this clutter, and handle the mapping of ViewModels -> views for you
Yes indeed, the issue with that is that i don't know the restrictions of this project yet.
This is going to be a Project for my Collage Module "Graphical User Interfaces" and the Proffesor hasn't disclosed rules about using Frameworks yet.
https://github.com/xAfterLife/505-GUI
GitHub
GitHub - xAfterLife/505-GUI: Re-Creating Battle-Ships in C# using t...
Re-Creating Battle-Ships in C# using the WPF Framework and MVVM - GitHub - xAfterLife/505-GUI: Re-Creating Battle-Ships in C# using the WPF Framework and MVVM
Aah, fair enough
Never the less thank you for your help!
Have a wonderfull day/evening 🙂
✅ This post has been marked as answered!