✅ How to bind an object observable property with mvvm community toolkit in wpf
I'm trying to migrate a wpf app to use the mvvm toolkit. I have a viewmodel and model setup that looks like this:
Now if I try to update a property of "Book" like in
ChangeTitle
or AddChapter
methods, the view doesn't update.
I guess ObservableProperty only works when assigning a new value directly to the tagged property. Because my LoadBook
method can definitely update the view. I'm binding my view like this:
How can I make it so that updating the inner properties of Book also updates the Book property itself?
Or maybe I shouldn't use the Book object directly and I should just expose the Book properties directly in the viewmodel like this below?
4 Replies
For what u want u would have to implement observability over book and chapter as well, so that updating the properties via code will reflect on the UI.
Do not mix model(entities efcore), dtos and viewmodels, specially in WPF as using dtos directly to represent viewmodels in WPF can cause memory leak and other side effects u want to avoid.
The idea in WPF is that u will create viewmodels to represent your view, it doesnt necessarily mean that it will be just a single viewmodel it can have child vms inside of it as well representing further down controls, lists and such.
Oh I see, I can tag the models directly :ThonkAnime: And yeah, this Book object is used within the wpf app only. By the time I get a book object from the service's Load() method, it's nothing more than an object wrapping around some copies of strings, lists, and ints.
I would still call it a viewmodel to uniform what its representing
as it could potentially have more stuff later down the road
but yes essentially u need to do that
If you have no further questions, please use /close to mark the forum thread as answered