title: Propagate / Bubble events across several ViewModel in MVVM
body: <p>I have a <code>MainWindow.xaml</code> with the <code>DataContext</code> set as <code>MainWindowViewModel.cs</code>. This window has nothing in it until the user clicks a button and adds a <code>MyUserControl.xaml</code> to the window. That <code>MyUserControl.xaml</code> has a <code>MyUserControlViewModel.cs</code> set as the <code>DataContext</code>.<br><br></p>
  
  <p>I want to trigger an event in the <code>MyUserControlViewModel.cs</code> that gets caught by the <code>MainWindowViewModel.cs</code>.<br><br></p>
  
  <p>So far I have this. In my <code>MyUserControlViewModel.cs</code></p>
  
  <pre><code>public event EventHandler&lt;LoadingEventArgs&gt; LoadingEvent;
  (...)
  
  public void MyFunction() {
      LoadingEvent(this, EventLibrary.LoadingEvent(2));
  }
  </code></pre>
  
  <p>And in my <code>MainWindowViewModel.cs</code></p>
  
  <pre><code>private MyUserControlViewModel _myUserControlViewModel;
  public MyUserControlViewModel _MyUserControlViewModel {
      get { return _myUserControlViewModel; }
      private set { _myUserControlViewModel = value; OnPropertyChangedEvent("MyUserControlViewModel"); }
  }
  
  public MainWindowViewModel() {
      // Can't attach event here
  }
  
  public void AddEventToUserControl() {
      _MyUserControlViewModel = new MyUserControlViewModel();
      _MyUserControlViewModel.LoadingEvent += MyUserControlViewModel_LoadingEvent;
  }
  
  private void MyUserControlViewModel_LoadingEvent(object sender, LoadingEventArgs e) {
      MessageBox.Show("I got here!");
  }
  </code></pre>
  
  <p>The message <code>"I got here"</code> never shows because the <code>LoadingEvent</code> in the <code>MyUserControlViewModel.cs</code> is <code>null</code>.<br>
  The reason I'm only attaching the <code>MyUserControlViewModel_LoadingEvent</code> outside the <code>MainWindowViewModel()</code> function is because the <code>MyUserControl.xaml</code> is not added when the windows loads, but only after, when the user clicks the button.<br>
  I think the problem here is that the event is not added as the same time as the creation of the <code>MyUserControlViewModel.cs</code>.<br><br>
  I need to know how can I trigger bubbling events through ViewModels when they are created dynamically through code.<br><br><br></p>
  
  <p><strong>UPDATE - MORE INFO ADDED</strong><br>
  Given that my above situation may be to simple and lacks important details, I'm going to describe my exact situation.<br><br></p>
  
  <p>I have three views - a grandfather, a father and a son. For each one I have a ViewModel class. In my father I have the son declared in the XAML because it is created at the same time, so when I have <code>SonViewModel = new SonViewModel()</code> in my <code>FatherViewModel</code>, it points to the right place and I can access everything I need in the <code>SonViewModel</code>. Consequently, any event triggered in <code>SonViewModel</code> that is being listened by <code>FatherViewModel</code> gets caught.<br>
  The two most important facts here are that both father and son are created at the same time and the son is created in the XAML like so:</p>
  
  <pre><code>&lt;View:SonView DataContext="{Binding SonViewModel, Mode=OneWay}" /&gt;
  </code></pre>
  
  <p>This works fine does exactly what I want.<br><br></p>
  
  <p>The problem comes with the grandfather and the father because they're not created at the same time. I first create the <code>GrandfatherView.xaml</code> and in it's associated <code>GrandfatherView.cs</code> (I know it's not MVVM practice, but I'm trying to implement some MVVM features in a project that's not with MVVM pattern) I create and add the <code>FatherView</code> to a <code>Grid</code>, like so:</p>
  
  <pre><code>FatherView _fatherView = new FatherView();
  _fatherView.DataContext = new FatherViewModel();
  mainGrid.Children.Add(_fatherView);
  </code></pre>
  
  <p>Because I'm creating the <code>FatherView</code> after I created the <code>GrandfatherView</code> and adding it through code and not having a specific binding, when I create the <code>FatherViewModel</code> object in my <code>GrandfatherViewModel</code> class, it's not pointing to the actual ViewModel of the <code>FatherView</code> I see in my app.<br>
  I'm not sure how to create the binding <code>&lt;View:SonView DataContext="{Binding SonViewModel, Mode=OneWay}" /&gt;</code> in code, so I can make sure the attributes and bindings are the same, so the ViewModel object I have in my <code>GrandfatherViewModel</code> is actually the one I see in my app and not a completely new object.<br><br></p>
  
  <p>So, any suggestions?</p>