title: Set the DataContext of a ComboBox in XAML
body: <p>This is part of a WPF MVVM application. I believe the problem I am having with this is that the data context is not clear. This data grid's ItemsSource is a collection of ViewModels. This is a property in a ViewModel used to display the rest of the data. So all the data is showing up and even individual items in the SpecimentViewModel. The only thing not working is I cannot display the chosen option in the comboBox. Since I have been messing with this I cannot even see "Select Note" which is the default (Item 0). As I said, all of the other columns appear. They are all properties in each viewModel. So is my ComboBox not seeing the DataContext?</p>
  
  <pre><code>                &lt;DataGrid
                  ItemsSource="{Binding Specimens}"
                  Style="{StaticResource DataGridStyle}"
                  Background="Beige"
                  FontFamily="Verdana"
                  FontSize="10"
                  AutoGenerateColumns="False"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  AlternatingRowBackground="Beige"
                  AlternationCount="2"
                  AllowDrop="False"
                  CanUserAddRows="False"
                  DataContext=""&gt;
                  &lt;DataGrid.Columns&gt;
                      &lt;DataGridTextColumn Binding="{Binding TestNumber}"
                                          Header="No."
                                          Width=".05*" /&gt;
  
                      &lt;DataGridTextColumn Binding="{Binding PlyCount}"
                                          Header="Ply Count"
                                          Width=".14*" /&gt;
  
                      &lt;DataGridTextColumn Binding="{Binding PlyThickness}"
                                          Header="Ply Thickness"
                                          Width=".14*" /&gt;
  
                      &lt;DataGridTextColumn Binding="{Binding TearPerPly}"
                                          Width=".14*"&gt;
  
                          &lt;DataGridTextColumn.Header&gt;
                              &lt;TextBlock Text="{Binding DataContext.SpencerDartHeading, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /&gt;
                          &lt;/DataGridTextColumn.Header&gt;
                      &lt;/DataGridTextColumn&gt;
  
                      &lt;DataGridTemplateColumn
                          Header="Note"
                          Width=".14*"&gt;
                          &lt;DataGridTemplateColumn.CellTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;ComboBox ItemsSource="{Binding ResultOptions}"
                                            SelectedItem="{Binding SelectedNote, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                            SelectedValue="{Binding SelectedNote, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                                            IsSynchronizedWithCurrentItem="True"/&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/DataGridTemplateColumn.CellTemplate&gt;
                      &lt;/DataGridTemplateColumn&gt;
                  &lt;/DataGrid.Columns&gt;
              &lt;/DataGrid&gt;
  </code></pre>
  
  <p>The ViewModel looks like this;</p>
  
  <pre><code>    private Specimen specimen;
      private ObservableCollection&lt;string&gt; resultOptions = new ObservableCollection&lt;string&gt;();
      public Specimen Specimen
      {
          get
          {
              return specimen;
          }
          set
          {
              specimen = value;
              RaisePropertyChanged("Specimen");
          }
      }
  
      public string SelectedNote
      {
          get
          {
              if (specimen.ResultNote == null)
                  specimen.ResultNote = "Select Note";
              return specimen.ResultNote;
          }
          set
          {
              specimen.ResultNote = value;
              RaisePropertyChanged();
          }
      }
  
      public ObservableCollection&lt;string&gt; ResultOptions
      {
          get { return resultOptions; }
          set
          {
              resultOptions = value;
              RaisePropertyChanged();
          }
      }
  </code></pre>
  
  <p>And the Specimen Model simply has a few test properties and the ResultNote string property. So a TestViewModel has the properties for a single test, one of which is a collection of SpecimenViewModels called Specimens. Each of the SpecimenViewModels has a property which is a ResultNote. That result note is selected from a combo box which is filled with a collection of Options called ResultOptions which is in the SpeciimenViewModel. The selected value is put into the property "ResultNote."</p>