title: WPF Communication between User Controls
body: <p>I'm trying to find the best way to communicate between two User Controls. I have a main XAML window which contains two User Controls which in turn contain various controls. The Code behind of each User Control simply sets the DataContext to a View Model that is associated to it. The View Model contains objects that are bound to the controls.
  What I'd like to do is capture when a list box in User Control 1 changes selection, the new selected item be displayed in an edit box in User Control 2. As I'm using View Models I can't declare Dependency Properties so I was wondering what is the accepted way to perform this?
  I've attached some basic code to show how I'm setting the controls.</p>
  
  <p>Main Window XAML</p>
  
  <pre><code>&lt;Window x:Class="CommsTest.View.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:CommsTest.View"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;local:UserControl1 /&gt;
      &lt;local:UserControl2 /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>UserControl1 XAML</p>
  
  <pre><code>&lt;UserControl x:Class="CommsTest.View.UserControl1"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300"&gt;
  &lt;Grid&gt;
      &lt;ComboBox Height="23" HorizontalAlignment="Left" Margin="50,110,0,0" Name="comboBox1" VerticalAlignment="Top" Width="199" ItemsSource="{Binding Combo1}" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>UserControl1ViewModel.cs</p>
  
  <pre><code>class UserControl1ViewModel
  {
      private ObservableCollection&lt;string&gt; combo1 = new ObservableCollection&lt;string&gt;();
  
      public ObservableCollection&lt;string&gt; Combo1
      {
          get { return combo1; }
      }
  }
  </code></pre>
  
  <p>UserControl2.XAML</p>
  
  <pre><code>&lt;UserControl x:Class="CommsTest.View.UserControl2"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300"&gt;
  &lt;Grid&gt;
      &lt;TextBox Height="23" HorizontalAlignment="Left" Margin="63,84,0,0" Name="textBox1" VerticalAlignment="Top" Width="170" Text="{Binding Path=Text1}" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>UserControl2ViewModel.cs</p>
  
  <pre><code>class UserControl2ViewModel
  {
      private string text1;
  
      public string Text1
      {
          get { return text1; }
          set { text1 = value; }
      }
  }
  </code></pre>
  
  <p>How do I get UserControl2.Text1 to be the selected value of UserControl2.Combo1?
  Thanks</p>