title: 2 related comboboxes in wpf DataGrid MVVM
body: <p>Does anyone have an example of binding a 2 related ComboBoxs to a Selected Item of a DataGrid? I have tried, but I can't seem to get it to work. I am fairly new to MVVM so I am doing something wrong. </p>
  
  <p>I have a datagrid which is binded to a collection in an object named <code>NewClient</code> in my ViewModel
  <code>ClientMarketCodes</code>. 
  <code>ClientMarketCodes</code> has a navigation property named <code>MarketCodeTypes</code> which has a Market Navigation property. so I want to show in the datagrid the code,<code>MarketCodeTyeName</code>,<code>MarketName</code>.
  I have Managed to show <code>Markets</code> and <code>MarketCodeTypes</code> in DataGrid comboboxes, but I want to do a relation between <code>Markets</code> and <code>MaketCodeTypes</code> where user can only select <code>MarketCodeTypes</code> related to there <code>Market</code>.
  I have added a selected item to <code>Markets</code> Combobox where on changing it excute filling method of <code>MarketCodeTypes</code> collection binded to <code>MarketCodeTypes</code> Combobox but it doesnt work correctly as the filter applys to all rows of <code>MarketCodetypes</code> Combobox column. 
  I have something that happens in both combobox for example on changing the <code>Markets</code> of one row the combobox shows all other markets I choosed one other Market and click the row is not affected and returns back to its original Market.i dont know why this happens.</p>
  
  <p>Heres my ViewModel:</p>
  
  <pre><code>public class MarketsViewModel : ObservableObject, IMarketsViewModel, INavigationAware, IConfirmNavigationRequest, IRegionMemberLifetime
  {
      #region MarketsViewModel
  
      public MarketsViewModel()
      {
          //NewClient.ClientMarketCodes[0].MarketCodeType.MarketID
          //MarketCodeTypes = new ObservableCollection&lt;MarketCodeType&gt;();
          this.GetMarkets();
          //this.GetMarketCodeTypes();
          this.PropertyChanged += MarketssViewModel_PropertyChanged;
      }
  
      private void MarketssViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
      {
          switch (e.PropertyName)
          {
              case "selectedMarket":
              {
                  if (selectedMarket != null)
                  {
                      GetMarketCodeTypes();
                  }
  
                  break;
              }
          }
      }
  
      #endregion
  
      #region --Members--
  
      private ObservableCollection&lt;Market&gt; m_Market;
      private ObservableCollection&lt;MarketCodeType&gt; m_MarketCodeType;
      private List&lt;MarketCodeType&gt; m_MarketCodeTypes;
  
      public List&lt;MarketCodeType&gt; MarketCodeTypesList
      {
          get
          {
              if (m_MarketCodeTypes == null)
              {
                  m_MarketCodeTypes = new List&lt;MarketCodeType&gt;();
              }
  
              return m_MarketCodeTypes;
          }
          set
          { 
              m_MarketCodeTypes = value;
  
              RaisePropertyChanged("MarketCodeTypesList");
              RaisePropertyChanged("MarketCodeTypes");
          }
      }
  
      public ObservableCollection&lt;MarketCodeType&gt; MarketCodeTypes
      {
          get
          {
              if (MarketCodeTypesList != null)
                  return new ObservableCollection&lt;MarketCodeType&gt;(MarketCodeTypesList);
              else
                  return new ObservableCollection&lt;MarketCodeType&gt;();
          }
      }
  
      #endregion
  
      #region --Properties--
  
      public ObservableCollection&lt;Market&gt; Markets
      {
          get { return m_Market; }
          set
          {
              m_Market = value;
              RaisePropertyChanged("Markets");
          }
      }
  
      private Market _selectedMarket;
      public Market selectedMarket
      {
          get { return _selectedMarket; }
          set
          {
              if (_selectedMarket != value)
              {
                  _selectedMarket = value;
                  RaisePropertyChanged("selectedMarket");
                  RaisePropertyChanged("MarketCodeTypes");
              }
          }
      }
  
      #endregion
  
      private void GetMarkets()
      {
          try
          {
              Market[] MarketArr;
  
              using (var client = new ClientServiceProxy())
              {
                  MarketArr = client.GetAllMarkets();
              }
              if (MarketArr != null)
              {
                  this.Markets = new ObservableCollection&lt;Market&gt;(MarketArr);
              }
          }
          catch (Exception e)
          {
              MessageBox.Show(e.Message);
          }
      }
  
      private void GetMarketCodeTypes()
      {
          try
          {
              MarketCodeType[] MarketCodeTypeArr;
              using (var client = new ClientServiceProxy())
              {
                  MarketCodeTypeArr = client.GetAllMarketCodeTypes().Where(c=&gt;c.MarketID==selectedMarket.marketID).ToArray();
              }
              if (MarketCodeTypeArr != null)
              {
                  MarketCodeTypesList = MarketCodeTypeArr.ToList();
              }
              else
              { MarketCodeTypesList = new List&lt;MarketCodeType&gt;(); }
  
          }
          catch (Exception e)
          {
              MessageBox.Show(e.Message);
          }
      }
  }`
  </code></pre>
  
  <p>Here's My Xaml:</p>
  
  <pre><code> &lt;Grid x:Name="grid2" RenderTransformOrigin="0.5,0.5"&gt;
  
          &lt;Custom:C1DataGrid x:Name="c1DataGrid"  
                             AutoGenerateColumns="False"
                             Height="490"  
                             ItemsSource="{Binding Path=NewClient.ClientMarketCodes,Mode=TwoWay,UpdateSourceTrigger=LostFocus,ValidatesOnDataErrors=True,ValidatesOnExceptions=True}"&gt;
                  &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                      &lt;GradientStop Color="#FFBEBE00" Offset="0.5"/&gt;
                      &lt;GradientStop Color="#FFEDED9A" Offset="0.496"/&gt;
                  &lt;/LinearGradientBrush&gt;
              &lt;/Custom:C1DataGrid.HeaderBackground&gt;
              &lt;Custom:C1DataGrid.Columns&gt;
                  &lt;!--&lt;Custom:DataGridCheckBoxColumn  Header="???? ?????"/&gt;--&gt;
                  &lt;Custom:DataGridTextColumn Header="Code" Binding="{Binding Code,Mode=TwoWay}" IsReadOnly="False"  /&gt;
  
                  &lt;Custom:DataGridTemplateColumn Header="MarketCodeType" &gt;
                      &lt;Custom:DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;TextBlock Text="{Binding MarketCodeType.NameA}"/&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/Custom:DataGridTemplateColumn.CellTemplate&gt;
                      &lt;Custom:DataGridTemplateColumn.CellEditingTemplate&gt;
                          &lt;DataTemplate&gt;
                            &lt;ComboBox  Name="cmbMarketCodeTypes" 
                                                   ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.MarketCodeTypes}" 
                                                   DisplayMemberPath="NameA" 
                                                   SelectedValue="{Binding Path=MarketCodeTypeID,Mode=TwoWay,ValidatesOnDataErrors=True}" 
                                                  /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/Custom:DataGridTemplateColumn.CellEditingTemplate&gt;
                  &lt;/Custom:DataGridTemplateColumn&gt;
  
                  &lt;Custom:DataGridTemplateColumn Header="Markets"&gt;
                      &lt;Custom:DataGridTemplateColumn.CellTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;TextBlock Text="{Binding MarketCodeType.Market.NameA}"/&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/Custom:DataGridTemplateColumn.CellTemplate&gt;
                      &lt;Custom:DataGridTemplateColumn.CellEditingTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;ComboBox Name="cmbMarkets" ItemsSource= "{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.Markets,Mode=TwoWay}" 
    DisplayMemberPath="NameA" SelectedValue="{Binding Path=MarketCodeType.Market.MarketID,Mode=TwoWay}" 
                  SelectedValuePath="MarketCodeType.MarketID"
         SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=my:MarketsView},Path=DataContext.selectedMarket,Mode=TwoWay}"    /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/Custom:DataGridTemplateColumn.CellEditingTemplate&gt;
                  &lt;/Custom:DataGridTemplateColumn&gt; 
  
  
                  &lt;/Custom:DataGridTemplateColumn&gt;
              &lt;/Custom:C1DataGrid.Columns&gt;
  
          &lt;/Custom:C1DataGrid&gt;
  </code></pre>