How to update a ViewModel from another ViewModel in WPF MVVM?

I have an application which I use to select some stuffs from Git Repository.

This is the Basic Design of the application.

The whole window has a ViewModel, inside which i have defined two instances of view models A & B represending two user controls. The View Model B, is a status bar and only have a string in it which shows a text which can be anything. The View Model A, is another user control, where i can search for User Stories. So basically i want to show the totals from View Model A in View Model B in the UI. I tried injecting VM B into A, but that doesnt reflect in the window.How can i resolve this using WPF MVVM pattern?

This is the actual design of the application.

4

It’s hard to understand what you’re trying to achieve with your given explanation. However, from what I understand, you have a ContentControl or another UIElement with a Content attribute. You could instead add another ContentControl and split your window into two, so you have two ContentControls for the window.

<Window.DataContext>
    <StaticResource ResourceKey="ViewModelCollection" />
</Window.DataContext>
...
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="500" />
        <RowDefinition Height="100" />
    </Grid.RowDefinitions>

    <ContentControl
        Grid.Column="0"
        Grid.Row="0"
        Content="{Binding MainView.CurrentView}">
    </ContentControl>

    <ContentControl
        Grid.Column="0"
        Grid.Row="1"
        Content="{Binding StatusBar.CurrentView}">
    </ContentControl>
</Grid>

To show two different CurrentViews at once, you’ll need to add another class to your project. For example, you can create a StatusBarViewModel class with a RelayCommand to set the CurrentView of your status bar to the appropriate UserControl.

Furthermore, you can create a ViewModelCollection class that holds the MainViewModel and StatusBarViewModel as properties:

class ViewModelCollection
{
    public ViewModelCollection()
    {
        MainView = new MainViewModel();
        StatusBar = new StatusBarViewModel();
    }

    private MainViewModel _MainView;
    private StatusBarViewModel _StatusBar;

    public MainViewModel MainView
    {
        get { return _MainView; }
        set { _MainView = value; }
    }

    public StatusBarViewModel StatusBar
    {
        get { return _StatusBar; }
        set { _StatusBar = value; }
    }
}

Don’t forget to reference your ViewModelCollection in your App.xaml and set it as the DataContext for your window:

<viewModel:ViewModelCollection x:Key="ViewModelCollection"/>

This approach ensures that you can manage multiple views and view models effectively within your window, with each content area bound to its respective view model.

I assume you want “VM B” to know about the changes that are happening in “VM A”. You have couple of options to accomplish this without much effort, I will elaborate on the simplest one:

  1. Use a common state class (a manager class), which contains event that informs about changes
    Example:

    public class AppManager
    {
        public event EventHandler<string> AppNameChanged;
    
        public void ChangeAppName(string newName)
        {
            AppNameChanged?.Invoke(this, newName);
        }
    }
    
    public class ViewModelA
    {
        readonly AppManager _appManager;
        public List<string> AppNamesList { get; set; }
        private string _selectedName;
        public string SelectedName 
        { 
            get =>_selectedName;
            set
            {
                _selectedName = value;
                _appManager.ChangeAppName(value);
            }
        }
    
        public ViewModelA(AppManager appManager)
        {
            AppNamesList = new List<string>();
        }
    }
    
    public class ViewModelB
    {
        readonly AppManager _appManager;
    
        public ViewModelB(AppManager appManager)
        {
            _appManager = appManager;
            _appManager.AppNameChanged += (sender, newName) =>
            {
                Console.WriteLine($"New App Name : {newName}");
            };
        }
    }
    
  2. Use Messeging systems (for example from Community MVVM toolkit) to send notification. Example:

    public record AppNameMessage(string Name);
    public class Message : ValueChangedMessage<AppNameMessage>
    {
        public Message(string Name) : base(new AppNameMessage(Name))
        {
        }
    }
    
    
    public class ViewModelA
    {
        public List<string> AppNamesList { get; set; }
        private string _selectedName;
        public string SelectedName
        {
            get => _selectedName;
            set
            {
                _selectedName = value;
                WeakReferenceMessenger.Default.Send<AppNameMessage>(new( new (value)));
            }
        }
    }
    
    public class ViewModelB
    {
    
        public ViewModelB()
        {
            WeakReferenceMessenger.Default.Register<AppNameMessage>(this, (sender, message) =>
            {
                Console.WriteLine($"New App Name : {message.Name}");
            });
    
        }
    }
    

Hope that help you out

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật