title: Set DataContext for multiple controls with XAML
body: <p>I have different groups of controls bound to different categories of ViewModel classes.</p>
  
  <p>The ViewModels are </p>
  
  <ul>
  <li><code>MainViewModel</code> </li>
  <li><code>VideoViewModel</code> </li>
  <li><code>AudioViewModel</code></li>
  </ul>
  
  <hr>
  
  <p><strong>Question</strong></p>
  
  <p>How can I set the <code>DataContext</code> with XAML instead of C#?</p>
  
  <p><strong>1.</strong> I tried adding <code>DataContext="{Binding VideoViewModel}"</code> to the <code>ComboBox</code> XAML, but it didn't work and the items came up empty.</p>
  
  <p><strong>2.</strong> I also tried grouping all the <code>ComboBoxes</code> of a certain category inside a <code>UserControl</code> with the <code>DataContext</code>:</p>
  
  <pre><code>&lt;UserControl DataContext="{Binding VideoViewModel}"&gt;
      &lt;!-- ComboBoxes in here --&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>3.</strong> Also tried setting the <code>&lt;Window&gt;</code> <code>DataContext</code> to itself <code>DataContext="{Binding RelativeSource={RelativeSource Self}}"</code></p>
  
  <hr>
  
  <h3>Data Context</h3>
  
  <p>I'm currently setting the <code>DataContext</code> this way for the different categories of controls:</p>
  
  <pre><code>public MainWindow()
  {
      InitializeComponent();
  
      // Main
      this.DataContext =
      tbxInput.DataContext =
      tbxOutput.DataContext =
      cboPreset.DataContext =
      MainViewModel.vm;
  
      // Video
      cboVideo_Codec.DataContext =
      cboVideo_Quality.DataContext =
      tbxVideo_BitRate.DataContext =
      cboVideo_Scale.DataContext =
      VideoViewModel.vm;
  
      // Audio
      cboAudio_Codec.DataContext =
      cboAudio_Quality.DataContext =
      tbxAudio_BitRate.DataContext =
      tbxAudio_Volume.DataContext =
      AudioViewModel.vm;
  }
  </code></pre>
  
  <hr>
  
  <h3>XAML ComboBox</h3>
  
  <pre><code>&lt;ComboBox x:Name="cboVideo_Quality" 
            DataContext="{Binding VideoViewModel}"
            ItemsSource="{Binding Video_Quality_Items}"
            SelectedItem="{Binding Video_Quality_SelectedItem}"
            IsEnabled="{Binding Video_Quality_IsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            HorizontalAlignment="Left" 
            VerticalAlignment="Top" 
            Width="105" 
            Height="22"
            Margin="0,0,0,0"/&gt;
  </code></pre>
  
  <hr>
  
  <h3>Video ViewModel Class</h3>
  
  <pre><code>public class VideoViewModel : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged = delegate { };
      private void OnPropertyChanged(string prop)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
  
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(prop));
          }
      }
  
  
      public VideoViewModel() { }
  
  
      public static VideoViewModel _vm = new VideoViewModel();
      public static VideoViewModel vm
      {
          get { return _vm; }
          set
          {
              _vm = value;
          }
      }
  
      // Items Source
      private List&lt;string&gt; _Video_Quality_Items = new List&lt;string&gt;()
      {
          "High",
          "Medium",
          "Low",
      };
      public List&lt;string&gt; Video_Quality_Items
      {
          get { return _Video_Quality_Items; }
          set
          {
              _Video_Quality_Items = value;
              OnPropertyChanged("Video_Quality_Items");
          }
      }
  
      // Selected Item
      private string _Video_Quality_SelectedItem { get; set; }
      public string Video_Quality_SelectedItem
      {
          get { return _Video_Quality_SelectedItem; }
          set
          {
              if (_Video_Quality_SelectedItem == value)
              {
                  return;
              }
  
              _Video_Quality_SelectedItem = value;
              OnPropertyChanged("Video_Quality_SelectedItem");
          }
      }
  
      // Enabled
      private bool _Video_Quality_IsEnabled;
      public bool Video_Quality_IsEnabled
      {
          get { return _Video_Quality_IsEnabled; }
          set
          {
              if (_Video_Quality_IsEnabled == value)
              {
                  return;
              }
  
              _Video_Quality_IsEnabled = value;
              OnPropertyChanged("Video_Quality_IsEnabled");
          }
      }
  }
  </code></pre>