Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 191481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:20:42+00:00 2026-05-11T16:20:42+00:00

I need to figure out how to communicate between ViewModels. I’m new to MVVM

  • 0

I need to figure out how to communicate between ViewModels. I’m new to MVVM so please be kind.

Here’s a dumbed down example

class definitions(assume that I have hooked the Child.PropertyChanged event in the ParentViewModel):

public class ParentViewModel : ViewModelBase
{
    public ChildViewModel Child { get; set; }
}

public class ChildViewModel : ViewModelBase
{
    String _FirstName;
    public String FirstName 
    {
        get { return _FirstName; }
        set
        {
            _FirstName = value;
            OnPropertyChanged("FirstName");
        }
    }
}

Here’s what you see in the resource dictionary

<DataTemplate DataType="{x:Type vm:ParentViewModel}">
    <vw:ParentView/>
</DataTemplate>

<DataTemplate DataType="{x:Type vm:ChildViewModel}">
    <vw:ChildView/>
</DataTemplate>

and the code-behind of the ChildView:

public partial class ChildView : UserControl
{
    public QueueView()
    {
        InitializeComponent();
        DataContext = new ChildViewModel();
    }
}

The obvious problem is that when the ChildView gets instantiated (via selection from the DataTemplate) it creates a new ChildViewModel class and the ParentViewModel doesn’t have access to it.

So how can I instantiate the DataContext of the View to be the original ViewModel that caused the DataTemplate to be selected?

An obvious fix is to mmerge the properties in the ChildViewModel into the ParentViewModel, but I would rather separate it because for reuse.

I’m sure the answer is trivial, I just would like to know what it is. 🙂

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:20:42+00:00Added an answer on May 11, 2026 at 4:20 pm

    You should simply remove the line:

    DataContext = new ChildViewModel();
    

    The DataContext of the view will be set automatically by WPF. DataTemplates always have their data context set to the data for the template (in this case the ViewModel):

    <DataTemplate DataType="{x:Type vm:ChildViewModel}">
        <vw:ChildView/>
    </DataTemplate>
    

    The end result is that you can build your view model objects separately (both parent and child classes) and then display them later by simply plugging them into content controls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've had an ongoing need here that I can't figure out how to address
EXAMPLE: http://jsbin.com/ewiko4/5/ So I have some Dynamic divs that I need to figure out
I need to figure out what div is visible out of four possible divs
I need to figure out the hard drive name for a solaris box and
I need to figure out how to get the data from D3D textures and
I UIButton using + buttonWithType: What I need to figure out is how to
I'm working on a web-application and need to figure out the best way to
I have an ASP.NET page where I need to figure out where the style
I suck at math. I need to figure out how to calculate a video
I have Windows Vista MCML app, and I need to figure out the current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.