title: Silverlight MVVM - How Can I Declaratively Bind a ComboBox in a DataGrid CellEditingTemplate using a ViewModel?
body: <p>I am trying to declaratively bind a ComboBox within a DataGrid CellEditingTemplate using a ViewModel.  The ComboBox is not being bound.  What am I doing wrong?</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;UserControl
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
      xmlns:data="clr-namespace:SilverlightApplication1"
      mc:Ignorable="d"
      x:Class="SilverlightApplication1.EmployeeDetail"
      Width="640" Height="480"&gt;
  
      &lt;UserControl.Resources&gt;
          &lt;data:EmployeeDetailsViewModel
              x:Key="ViewModel"
              d:IsDataSource="True" /&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource ViewModel}}" Background="White"&gt;
  
          &lt;sdk:DataGrid ItemsSource="{Binding Employees,Mode=TwoWay}" AutoGenerateColumns="False" CanUserSortColumns="True" CanUserReorderColumns="True" CanUserResizeColumns="True" GridLinesVisibility="All" Height="317" HorizontalAlignment="Left" Margin="12,136,0,0" Name="EmployeesGrid" VerticalAlignment="Top" Width="605"&gt;
              &lt;sdk:DataGrid.Columns&gt;
  
  
  &lt;!-- snipped from brevity --&gt;
  
                  &lt;sdk:DataGridTemplateColumn Header="Status"&gt;
                      &lt;sdk:DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;TextBlock Text="{Binding EmployeeStatus.Description}" TextWrapping="Wrap"&gt;&lt;/TextBlock&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/sdk:DataGridTemplateColumn.CellTemplate&gt;
                      &lt;sdk:DataGridTemplateColumn.CellEditingTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;ComboBox ItemsSource="{Binding Path=EmployeeStatuses}" SelectedItem="{Binding EmployeeStatus, Mode=TwoWay}" /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/sdk:DataGridTemplateColumn.CellEditingTemplate&gt;
                  &lt;/sdk:DataGridTemplateColumn&gt;
  
              &lt;/sdk:DataGrid.Columns&gt;
          &lt;/sdk:DataGrid&gt;
          &lt;TextBlock x:Name="SearchLabel" HorizontalAlignment="Left" Margin="12,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="106" Height="34"&gt;&lt;Run FontWeight="Bold" Text="Search By Name: "/&gt;&lt;Run FontSize="9.333" Text="(Last, First)"/&gt;&lt;/TextBlock&gt;
          &lt;TextBox x:Name="SearchParam" HorizontalAlignment="Left" Margin="144,101,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="162"/&gt;
          &lt;Button x:Name="SearchButton" Content="Search" HorizontalAlignment="Right" Margin="0,102,242,0" VerticalAlignment="Top" Width="75" Click="SearchButton_Click"/&gt;    
  
  
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>VIEW MODEL:</p>
  
  <pre><code>using System.Collections.ObjectModel;
  using SilverlightApplication1.EmployeeService;
  using SilverlightApplication1.ViewModels;
  
  namespace SilverlightApplication1
  {
      public class EmployeeDetailsViewModel : ViewModelBase
      {
          readonly IEmployeeServiceAgent _serviceAgent;
          ObservableCollection&lt;EmployeeStatus&gt; _employeeStatuses { get; set; }
          ObservableCollection&lt;Employee&gt; _employees { get; set; }
  
          public EmployeeDetailsViewModel() : this(new EmployeeServiceAgent()) { }
          public EmployeeDetailsViewModel(IEmployeeServiceAgent serviceAgent)
          {
              if (!IsDesignTime)
              {
                  _serviceAgent = serviceAgent;
                  GetAllEmployees();
                  GetEmployeeStatuses();
              }
  
          }
  
          public ObservableCollection&lt;Employee&gt; Employees
          {
              get { return _employees; }
              set
              {
                  if(_employees!=value)
                  {
                      _employees = value;
                      OnNotifyPropertyChanged("Employees");
                  }
              }
          }
  
          public ObservableCollection&lt;EmployeeStatus&gt; EmployeeStatuses
          {
              get { return _employeeStatuses; }
              set
              {
                  if (_employeeStatuses != value)
                  {
                      _employeeStatuses = value;
                      OnNotifyPropertyChanged("EmployeeStatuses");
                  }
              }
          }
  
          private void GetAllEmployees()
          {
              _serviceAgent.GetAll((s, e) =&gt; Employees = e.Result);
          }
  
          private void GetEmployeeStatuses()
          {
              _serviceAgent.GetEmployeeStatuses((s, e) =&gt; EmployeeStatuses = e.Result);
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Update:</strong></p>
  
  <p>This seems wrong but I figured out how to get the binding working by re-referencing the ViewModel in the ItemSource Binding:</p>
  
  <pre><code>&lt;ComboBox ItemsSource="{Binding Source={StaticResource ViewModel},Path=EmployeeStatuses}"
                                        DisplayMemberPath="Description"
                                        SelectedItem="{Binding EmployeeStatus, Mode=TwoWay}"  /&gt;
  </code></pre>
  
  <p>However, a am now experiencing a problem where the SelectedItem is not bound!  What am I doing wrong?</p>