© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•7mo ago•
7 replies
HFrederick

✅ 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:
public partial class MyViewmodel : ObservableObject
{
    [ObservableProperty]
    private Book? book;
    
    [RelayCommand]
    public void LoadBook()
    {
        Book = FileLoaderService.Load("filepathhere.zip");
    }
    
    [RelayCommand]
    public void AddChapter()
    {
        Book.Chapters.Add(new Chapter("New chapter", "new content"));
    }
    [RelayCommand]
    public void ChangeTitle()
    {
        Book.Title = "New title";
    }
}

public class Book
{
    public string Title {get;set;}
    public List<Chapter> Chapters {get;};
}

public class Chapter
{
    public string ChapterTitle {get;}
    public string Content {get;}
}
public partial class MyViewmodel : ObservableObject
{
    [ObservableProperty]
    private Book? book;
    
    [RelayCommand]
    public void LoadBook()
    {
        Book = FileLoaderService.Load("filepathhere.zip");
    }
    
    [RelayCommand]
    public void AddChapter()
    {
        Book.Chapters.Add(new Chapter("New chapter", "new content"));
    }
    [RelayCommand]
    public void ChangeTitle()
    {
        Book.Title = "New title";
    }
}

public class Book
{
    public string Title {get;set;}
    public List<Chapter> Chapters {get;};
}

public class Chapter
{
    public string ChapterTitle {get;}
    public string Content {get;}
}


Now if I try to update a property of "Book" like in
ChangeTitle
ChangeTitle
or
AddChapter
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
LoadBook
method can definitely update the view. I'm binding my view like this:

<TextBlock Content="{Binding Book.Title}" />
<ListBox ItemsSource={Binding Book.Chapters}>
    <TextBlock Content="{Binding ChapterTitle}" />
    <TextBlock Content="{Binding Content}" />
</ListBox>
<TextBlock Content="{Binding Book.Title}" />
<ListBox ItemsSource={Binding Book.Chapters}>
    <TextBlock Content="{Binding ChapterTitle}" />
    <TextBlock Content="{Binding Content}" />
</ListBox>


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?
public partial class MyViewmodel : ObservableObject
{
    [ObservableProperty]
    private string title;

    [ObservableProperty]
    private ObservableCollection<Chapter> chapters;

    // ...
}
public partial class MyViewmodel : ObservableObject
{
    [ObservableProperty]
    private string title;

    [ObservableProperty]
    private ObservableCollection<Chapter> chapters;

    // ...
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Filtering an Observable Collection in WPF MVVM via ICollectionView
C#CC# / help
2y ago
MVVM TOOLKIT SetProtperty
C#CC# / help
3y ago
Microsoft MVVM toolkit
C#CC# / help
3y ago
✅ MVVM: Are models observable?
C#CC# / help
3y ago