title: databinding combobox in Dataform Silverlight using MVVM in Update
body: <p>I have Master/Detail – datagrid/dataform and after select item it shows in dataform for update, but I have a problem with databinding or populating combox with departments and set SelectedEmployee.departmentid as selectedvalue. </p>
  
  <p>Here 2 questions now:</p>
  
  <p><strong>1.    In EmployeeViewModel this code just doesn’t work and the question why?</strong></p>
  
  <pre><code>  private ObservableCollection&lt;department&gt; _departments;
          public ObservableCollection&lt;department&gt; Departments
          {
              get { return _departments; }
              set
              {
                  _departments = value;
                  RaisePropertyChanged("Departments");
              }
          }
  </code></pre>
  
  <p>But  this code works fine</p>
  
  <pre><code>   private ObservableCollection&lt;department&gt; _departments;
          public ObservableCollection&lt;department&gt; Departments
          {
              get {
  
                  if (_departments == null)
                  {
                      _departments = new ObservableCollection&lt;department&gt; {
                          new department()
                          { id = 1, departmentname = "Technical " + 1, },
                          new department()
                          { id = 2,  departmentname = "Technical " + 2, },
                          new department()
                          { id = 3,  departmentname = "Technical " + 3, }                    
                      };
                  }
                    return _departments; 
              }
              set
              {
                  _departments = value;
                  RaisePropertyChanged("Departments");
              }
          }
  </code></pre>
  
  <p><strong>2.    Behavior of Combobox inside and outside DataForm is different. Outside it works, inside it doesn’t. I think here need to use Source in ItemsSource, but I don’t know how. So there is another question is how to fix it?</strong></p>
  
  <p>employeeView.xaml</p>
  
  <pre><code>&lt;navigation:Page    xmlns:local="clr-namespace:departmentTechManager"
              x:Class="departmentTechManager.Views.employeeView" 
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                     mc:Ignorable="d"
                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
                     d:DesignWidth="820" d:DesignHeight="780"
                     Title="employees"
                     Style="{StaticResource PageStyle}"
  
              xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
              xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
              xmlns:mvvmlightcmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
              xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
              xmlns:dataformtoolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit" 
  
              DataContext="{Binding employeeStatic, Source={StaticResource Locator}}"&gt;
  
  &lt;data:DataGrid Grid.Row="0" x:Name="dgEmployees" CanUserSortColumns="true"
                                               IsReadOnly="true" AutoGenerateColumns="true"
                                               ItemsSource="{Binding Employees}"
                                               SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}"  Margin="0,36,-123,0"&gt;&lt;/data:DataGrid&gt;
  &lt;dataformtoolkit:DataForm x:Name="dfDetails"
        CurrentItem="{Binding SelectedEmployee}" AutoGenerateFields="False"
        CommitButtonContent="Save" CommandButtonsVisibility="Edit, Commit, Cancel"&gt;
        &lt;dataformtoolkit:DataForm.EditTemplate&gt;
                        &lt;DataTemplate&gt;
                          &lt;StackPanel&gt;
                             &lt;dataformtoolkit:DataField Label="name"&gt;
      &lt;TextBox Text="{Binding name, Mode=TwoWay}" /&gt;&lt;/dataformtoolkit:DataField&gt;
  
        &lt;dataformtoolkit:DataField Label="departments"&gt;
  
  
              &lt;ComboBox ItemsSource="{Binding Departments}"  
          DisplayMemberPath="departmentname"
              SelectedValuePath="id"  
              SelectedValue="{Binding Path=SelectedEmployee.departmentid, Mode=TwoWay}" /&gt;
  
  
       &lt;/dataformtoolkit:DataField&gt;
        &lt;/StackPanel&gt;
       &lt;/DataTemplate&gt;
              &lt;/dataformtoolkit:DataForm.EditTemplate&gt;
              &lt;i:Interaction.Triggers&gt;&lt;i:EventTrigger EventName="EditEnded"&gt;
                  &lt;mvvmlightcmd:EventToCommand Command="{Binding SaveEmployeesCommand}"/&gt;
                 &lt;/i:EventTrigger&gt;&lt;/i:Interaction.Triggers&gt;
              &lt;/dataformtoolkit:DataForm&gt;
  </code></pre>
  
  <p>In ViewModelLocator.cs:</p>
  
  <pre><code> public ViewModelLocator()
          {
  
              _sp = ServiceProviderBase.Instance;
  
              Createdepartment();
  
              Createemployee();
  
          }
  
  #region EmployeeViewModel
  private static EmployeeViewModel _employee;
  
  public static EmployeeViewModel employeeStatic
  {
      get
      {
          if (_employee == null)
          {
              Createemployee();
          }
  
          return _employee;
      }
  }
  
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
      "CA1822:MarkMembersAsStatic",
      Justification = "This non-static member is needed for data binding purposes.")]
  public EmployeeViewModel employee
  {
      get
      {
          return employeeStatic;
      }
  }
  
  public static void Clearemployee()
  {
      //do it later
      //_employee.Cleanup();
      _employee = null;
  }
  
  public static void Createemployee()
  {
      if (_employee == null)
      {
          _employee = new EmployeeViewModel(_sp.PageConductor, _sp.EmployeeDataService);
      }
  }
  #endregion
  
  #region DepartmentViewModel
          private static DepartmentViewModel _department;
  
          public static DepartmentViewModel departmentStatic
          {
              get
              {
                  if (_department == null)
                  {
                      Createdepartment();
                  }
  
                  return _department;
              }
          }
  
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance",
              "CA1822:MarkMembersAsStatic",
              Justification = "This non-static member is needed for data binding purposes.")]
          public DepartmentViewModel department
          {
              get
              {
                  return departmentStatic;
              }
          }
  
          public static void Cleardepartment()
          {
              //do it later
              //_department.Cleanup();
              _department = null;
          }
  
          public static void Createdepartment()
          {
              if (_department == null)
              {
                  _department = new DepartmentViewModel(_sp.PageConductor, _sp.DepartmentDataService);
              }
          }
  
          #endregion
  </code></pre>
  
  <p><em>Can somebody help me?</em></p>
  
  <p><strong>combox is just empty. but now i can populate it with Departments like so:</strong></p>
  
  <p>departmentTechManagerDomainService.metadata.cs</p>
  
  <pre><code> [MetadataTypeAttribute(typeof(employee.employeeMetadata))]
          public partial class employee
          {
       [Include]
       public department department { get; set; }
       public Nullable&lt;int&gt; departmentid { get; set; }
       public string name { get; set; }
          } 
  </code></pre>
  
  <p><strong>departmentTechManagerDomainService.cs</strong> </p>
  
  <pre><code>public IQueryable&lt;employee&gt; GetEmployees()
          {return this.ObjectContext.employees.Include("department").OrderBy(e=&gt;e.name);}
  </code></pre>
  
  <p><strong>here is ViewModel code:</strong></p>
  
  <pre><code>        private ObservableCollection&lt;department&gt; _departments;
          public ObservableCollection&lt;department&gt; Departments
          {
              get { return _departments; }
              set
              {
                  _departments = value;
                  RaisePropertyChanged("Departments");
              }
          }
  
          private department _selectedDepartment;
          public department SelectedDepartment
          {
              get { return _selectedDepartment; }
              set
              {
                  _selectedDepartment = value;
                  RaisePropertyChanged("SelectedDepartment");
              }
          }
  
  private void InitializeModels()
          {
              Employees = new ObservableCollection&lt;employee&gt;();
              SelectedEmployee = new employee();
              NewEmployee = new employee();
  
              //new
              Departments = new ObservableCollection&lt;department&gt;();
              SelectedDepartment = new department();
  
          }
  
  private void GetEmployeesCallback(IEnumerable&lt;employee&gt; employees)
          {
              if (employees != null)
              {
                  foreach (var employee in employees)
                  {
                      Employees.Add(employee);
                      //new
                      if (!Departments.Contains(employee.department))
                          Departments.Add(employee.department);
  
                  }
                  if (Employees.Count &gt; 0)
                  {
                      SelectedEmployee = Employees[0];
                  }
  
              }
          }
  </code></pre>
  
  <p><strong>I make Departments distinct, but here is only departments those already have been selected, but here aren't those that haven't been selected yet, and still combobox is not populated with departments in DataForm. ?!</strong></p>