title: Combobox inside DataGrid, hide selected item on Combobox
body: <p>What I am trying to accomplish is to have a combo box that in a row on the DataGrid. Each of the comboboxes are bound to the same ItemsSource. I would like to have the end user be able to click on an item in the combobox and for that specific combobox, have that item be the selected item. And the rest of the comboboxes will then hide the selected value. I am still new to XAML though I do  understand the whole MVVM pattern (which is what I am using). </p>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;DataGridTemplateColumn Header="Database Column" Width="150"&gt;
    &lt;DataGridTemplateColumn.CellTemplate&gt;
      &lt;DataTemplate&gt;
        &lt;ComboBox ItemsSource="{Binding DataContext.DatabaseMappingFields, ElementName=Root}" DisplayMemberPath="ColumnName"&gt;
          &lt;ComboBox.Resources&gt;
            &lt;Style TargetType="ComboBoxItem"&gt;
              &lt;Setter Property="Visibility" Value="{Binding IsSelected, Converter={StaticResource InverseBoolToVis}}"/&gt;
            &lt;/Style&gt;
          &lt;/ComboBox.Resources&gt;
          &lt;i:Interaction.Triggers&gt;
            &lt;i:EventTrigger EventName="SelectionChanged"&gt;
              &lt;command:EventToCommand Command="{Binding DataContext.CommandDatabaseHeaderSelected, ElementName=Root}"  PassEventArgsToCommand="True"/&gt;
            &lt;/i:EventTrigger&gt;
          &lt;/i:Interaction.Triggers&gt;
        &lt;/ComboBox&gt;
      &lt;/DataTemplate&gt;
    &lt;/DataGridTemplateColumn.CellTemplate&gt;
  &lt;/DataGridTemplateColumn&gt;
  </code></pre>
  
  <p><strong>C#:</strong></p>
  
  <pre><code>SetDatabaseColumns(new List&lt;string&gt;()
  { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6" });
  
  public void SetDatabaseColumns(List&lt;string&gt; databaseColumnNames)
      {
          DatabaseMappingFields.Clear();
          DatabaseMappingFields.Add(new DatabaseColumnsModel() { ColumnName = "&lt;ignore&gt;" });
          foreach (var item in databaseColumnNames)
          {
              DatabaseMappingFields.Add(new DatabaseColumnsModel() { ColumnName = item });
          }
      }
  
  public ICommand CommandDatabaseHeaderSelected
      {
          get
          {
              return _commandDatabaseHeaderSelected ??
                     (_commandDatabaseHeaderSelected = new RelayCommand&lt;SelectionChangedEventArgs&gt;(DatabaseHeaderSelected));
          }
      }
  
  private void DatabaseHeaderSelected(SelectionChangedEventArgs args)
      {
          var selectedItem = args.AddedItems.Count &gt;= 1
              ? args.AddedItems[0] as DatabaseColumnsModel
              : null;
          var previousItem = args.RemovedItems.Count &gt;= 1
              ? args.RemovedItems[0] as DatabaseColumnsModel
              : null;
          var selectedFileMapping = Data.TextFileMapping.FirstOrDefault(w =&gt; w == SelectedFileMapping);
  
          if (selectedItem == null) return;
  
          var isSelected = IsSelectedItemEnabled(selectedItem, previousItem);
          selectedItem.IsSelected = isSelected;
  
          selectedFileMapping.DatabaseColumn = isSelected ? selectedItem : null;
  
          if (previousItem == null) return;
          previousItem.IsSelected = false;
      }
  
  public class DatabaseColumnsModel : ObservableObject
  {
      private string _columnName;
      private bool _isSelected;
      private int _index;
  
      public int Index
      {
          get { return _index; }
          set { Set(() =&gt; Index, ref _index, value); }
      }
  
      public string ColumnName
      {
          get { return _columnName; }
          set { Set(() =&gt; ColumnName, ref _columnName, value); }
      }
  
      public bool IsSelected
      {
          get { return _isSelected; }
          set { Set(() =&gt; IsSelected, ref _isSelected, value); }
      }
  }
  </code></pre>