title: WPF Passing data object from Main application UI to user control
body: <p>I have user controls defined to represent the contents of tab items so as to split up a large XAML file into smaller files. I would like to pass reference to a data object from the main UI class to the user controls.</p>
  
  <p>I understand that DependancyProperties and RelativeSource's are ways to achieve this but am not sure how to implement this due to my lack of WPF expertise. Can someone help me.</p>
  
  <p>Thanks</p>
  
  <p>I have three xaml files, MainWindow, AlsTabUC (UserControl) and RangingTabUC (UserControl). I have a single object representing a device that performs both range and ambient light measurements and wojuld like to perform these activities in separate tabs.</p>
  
  <p>The object m_mySensorDevice is a member of MainWindow, which is the parent and I would like to pass this object to the two children so that they can execute readAmbientLight and readRange methods.</p>
  
  <p>Naturally, I have provided very basic sample code for illustration. In reality these tabs contain much more information, (along with other tabs) hence the reason for the user controls.</p>
  
  <p><strong>MainWindow - XAML</strong>
      
  
  <pre><code>    &lt;Window.Resources&gt;
          &lt;System:String x:Key="strTabHeaderRanging"&gt;Ranging&lt;/System:String&gt;
          &lt;System:String x:Key="strTabHeaderALS"&gt;ALS&lt;/System:String&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;TabControl Name="MainTab" TabStripPlacement="Top"
                      Margin="0,20,0,10"
                      SelectionChanged="mainTab_SelectionChanged" &gt;
              &lt;TabItem Name="tabItemRanging"
                       Header="{Binding Source={StaticResource strTabHeaderRanging}}"&gt;
              &lt;Grid&gt;
                  &lt;my:rangingTabUC HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   x:Name="rangingTabUC1"/&gt;
              &lt;/Grid&gt;
              &lt;/TabItem&gt;
              &lt;TabItem Name="tabItemAls"
                    Header="{Binding Source={StaticResource strTabHeaderALS}}"&gt;
                  &lt;Grid&gt;
                      &lt;my:AlsTabUC HorizontalAlignment="Center"
                                   VerticalAlignment="Center"
                                   x:Name="alsTabUC1" /&gt; 
                  &lt;/Grid&gt;
              &lt;/TabItem&gt;
          &lt;/TabControl&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>MainWindow - Code</strong></p>
  
  <pre><code>public partial class MainWindow : Window
  {
      SensorDevice m_mySensorDevice;
      public MainWindow()
      {
          m_mySensorDevice = new SensorDevice();
          InitializeComponent();
      }
      private void mainTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
      }
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
  
      }
  }
  
  public class SensorDevice
  {
  }
  </code></pre>
  
  <p><strong>AlsTabUC - XAML</strong></p>
  
  <pre><code>&lt;UserControl x:Class="TabUserControls.AlsTabUC"&gt;
      &lt;Grid&gt;
          &lt;Button Height="25" Width="100" Name="readAmbientLight"
                  HorizontalAlignment="Center"  VerticalAlignment="Center"
                  Click="readAmbientLight_Click" Margin="2"&gt;
              Read Amb Light
          &lt;/Button&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>AlsTabUC - Code</strong></p>
  
  <pre><code>public partial class AlsTabUC : UserControl
  {
      public AlsTabUC()
      {
          InitializeComponent();
      }
  
      private void readAmbientLight_Click(object sender, RoutedEventArgs e)
      {
          m_mySensorDevice.readAmbientLight();
      }
  }
  </code></pre>
  
  <p><strong>rangingTabUC- XAML</strong></p>
  
  <pre><code>&lt;UserControl x:Class="TabUserControls.rangingTabUC"&gt;
      &lt;Grid&gt;
          &lt;Button Height="25" Width="100" Name="readRange"
                  HorizontalAlignment="Center"  VerticalAlignment="Center"
                  Click="readRange_Click" Margin="2"&gt;
              Read Range
          &lt;/Button&gt;  
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>rangingTabUC- Code</strong></p>
  
  <pre><code>public partial class rangingTabUC : UserControl
  {
      public rangingTabUC()
      {
          InitializeComponent();
      }
  
      private void readRange_Click(object sender, RoutedEventArgs e)
      {
          m_mySensorDevice.readRange();
      }
  }
  </code></pre>