title: Proper Code-Behind Object Data Binding in XAML?
body: <p>I am currently binding an object in my code-behind (C#) to my XAML by giving a name to the XAML control, and setting the DataContext in the code-behind.</p>
  
  <pre><code>public partial class SmsControl: UserControl
  {
      private readonly DataOrganizer _dataOrganizer;
      public FunctionalTester _funcTester;
  
      public SmsControl()
      {
          InitializeComponent();
  
          _dataOrganizer = new DataOrganizer();
          _funcTester = new FunctionalTester();
  
          // Set the datacontext appropriately
          grpModemInitialization.DataContext = _funcTester;
      }
  
      private async void Button_Click_2(object sender, RoutedEventArgs e)
      {
          await _funcTester.Test();
      }
  }
  </code></pre>
  
  <p>And my XAML...</p>
  
  <pre><code> &lt;!-- SMS Test Modem Initialization GroupBox --&gt;
      &lt;GroupBox x:Name="grpModemInitialization" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3"  Style="{StaticResource groupboxViewItem}"&gt;
          &lt;GroupBox.Header&gt;
              &lt;Label Content="SMS Test Modem Initialization" Style="{StaticResource boldHeaderItem}"/&gt;
          &lt;/GroupBox.Header&gt;
  
          &lt;!-- SMS Test Modem Initialization Grid --&gt;
          &lt;Grid&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="*"/&gt;
                  &lt;RowDefinition Height="*"/&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="50"/&gt;
                  &lt;ColumnDefinition Width="*"/&gt;
                  &lt;ColumnDefinition Width="*"/&gt;
                  &lt;ColumnDefinition Width="*"/&gt;
                  &lt;ColumnDefinition Width="*"/&gt;
                  &lt;ColumnDefinition Width="*"/&gt;
              &lt;/Grid.ColumnDefinitions&gt;
  
              &lt;Label Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Content="COM:" Style="{StaticResource boldHeaderItem}" /&gt;
              &lt;ComboBox Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Style="{StaticResource comboBoxItem}" ItemsSource="{Binding AvailableCommPorts}" SelectedItem="{Binding SelectedCommPort}" /&gt;
              &lt;Label Grid.Column="2" Grid.Row="0" Content="Modem Ready:" Style="{StaticResource boldHeaderItem}" /&gt;
              &lt;Label Grid.Column="2" Grid.Row="1" Content="RSSI:" Style="{StaticResource boldHeaderItem}" /&gt;
              &lt;Label Content="{Binding ModemReady}" Grid.Column="3" Grid.Row="0"  HorizontalContentAlignment="Left" VerticalAlignment="Center"/&gt;
              &lt;Label Content="{Binding ModemRssi}" Grid.Column="3" Grid.Row="1" HorizontalContentAlignment="Left" VerticalAlignment="Center" /&gt;
              &lt;Label Grid.Column="4" Grid.Row="0" Content="Modem #:" Style="{StaticResource boldHeaderItem}"/&gt;
              &lt;Button Grid.Column="4" Grid.Row="1" Grid.ColumnSpan="2" Content="Initialize" /&gt;
              &lt;Label Content="{Binding ModemNumber}" Grid.Column="5" Grid.Row="0" HorizontalContentAlignment="Left" VerticalAlignment="Center"/&gt;
          &lt;/Grid&gt;
      &lt;/GroupBox&gt;
  </code></pre>
  
  <p>The code above works fine - no problems. What I'm asking is, if there is a way to set the DataContext of the GroupBox in XAML, referencing my _funcTester object, instead of setting the DataContext in the code-behind?  The reason I ask, is because different controls need to be bound to different objects in the code-behind and I'm not finding good resources on how to do so, except as I show above (giving a "x:Name" to each XAML control and setting the DataContext in code-behind).  Any help is appreciated!  Thanks!</p>