title: How to pass information from one WPF UserControl to another WPF UserControl?
body: <p>I've got a WPF application.</p>
  
  <p>On the left side there is a stackpanel full of buttons.</p>
  
  <p>On the right side there is an empty dockpanel.</p>
  
  <p>When user <strong>clicks a button</strong>, it <strong>loads the corresponding UserControl</strong> (View) into the dockpanel:</p>
  
  <pre><code>private void btnGeneralClick(object sender, System.Windows.RoutedEventArgs e)
  {
      PanelMainContent.Children.Clear();
      Button button = (Button)e.OriginalSource;
      Type type = this.GetType();
      Assembly assembly = type.Assembly;
  
      IBaseView userControl = UserControls[button.Tag.ToString()] as IBaseView;
      userControl.SetDataContext();
  
      PanelMainContent.Children.Add(userControl as UserControl);
  }
  </code></pre>
  
  <p><strong>This pattern works well</strong> since each UserControl is a View which has a ViewModel class which feeds it information which it gets from the Model, so the user can click from page to page and each page can carry out isolated functionality, such as editing all customers, saving to the database, etc.</p>
  
  <h2>Problem:</h2>
  
  <p>However, now, on one of these pages I want to have a ListBox with a list of Customers in it, and each customer has an "edit" button, and when that edit button is clicked, I want to fill the DockPanel with the EditSingleCustomer UserControl and pass it the Customer that it needs to edit.</p>
  
  <p>I can load the EditCustomer usercontrol, but how do I <em>pass it the customer to edit and set up its DataContext to edit that customer</em>?</p>
  
  <ul>
  <li>I can't pass it in the constructor since all the UserControls are already created and exist in a Dictionary in the MainWindow.xaml.cs.</li>
  <li>so I created a PrepareUserControl method on each UserControl and pass the Customer to it and can display it with a textbox from code behind with x:Name="..." but that is not the point, I need to DataBind an ItemsControl to a ObservableCollection to take advantage of WPF's databinding functionality of course.</li>
  <li>so I tried to bind the ListBox ItemSource in the View to its code behind like this:</li>
  </ul>
  
  <hr>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;local:ManageSingleCustomer x:Key="CustomersDataProvider"/&gt;
  &lt;/UserControl.Resources&gt;
  
  &lt;DockPanel&gt;
      &lt;ListBox ItemsSource="{Binding Path=CurrentCustomersBeingEdited, Source={StaticResource CustomersDataProvider}}"
               ItemTemplate="{DynamicResource allCustomersDataTemplate}"
               Style="{DynamicResource allCustomersListBox}"&gt;
      &lt;/ListBox&gt;
  &lt;/DockPanel&gt;
  </code></pre>
  
  <p>which gets a stackoverflow error caused by an endless loop in the IntializeComponent() in that view. <strong>So I'm thinking I'm going about this in the wrong way, there must be some easier paradigm to simply pass commands from one UserControl to another UserControl in WPF</strong> (and before someone says "use WPF commanding", I already am using commanding on my UserControl that allows the user to edit all customers, which works fine, but I have to handle it in my code behind of my view (instead of in my viewmodel) since I need the parent window context to be able to load another user control when its finished saving:</p>
  
  <pre><code>&lt;Button Style="{StaticResource formButton}" 
          Content="Save" 
          Command="local:Commands.SaveCustomer"
          CommandParameter="{Binding}"/&gt;
  
  private void OnSave(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
  {
      Customer customer = e.Parameter as Customer;
      Customer.Save(customer);
  
      MainWindow parentShell = Window.GetWindow(this) as MainWindow;
      Button btnCustomers = parentShell.FindName("btnCustomers") as Button;
      btnCustomers.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
  }
  </code></pre>
  
  <p><strong>So how in WPF can I simply have a UserControl loaded in a DockPanel, inside that UserControl a button with a command on it that loads another UserControl and <em>sends that UserControl a specific object to which it can bind its controls?</em></strong></p>
  
  <p>I can imagine I just do not know enough about WPF commands at this point, if anyone can point me in the right direction from here, that would be great, or if you think this "loading UserControls in a DockPanel pattern is foreign to WPF and should be avoided and replaced with another way to structure applications", that would be helpful news as well. You can <a href="http://tanguay.info/web/index.php?pg=codeExamples&amp;id=169" rel="nofollow noreferrer">download the current state of my application here</a> to get an idea of how it is structured. Thanks.</p>