title: WPF databinding colored items in a combobox
body: <p><strong>I have read a couple of other articles, but none have been able to answer my combination of issues</strong><br>
  I have a ComboBox in which i want to display items in different colors, this can be done by using a ComboBoxItem and settings its Background. My problem arises when i want to store my CategoryDTOs in different colors and later be able to extract them again. What i need to have displayed is just the color and the Name property of my CategoryDTOs. And i must then be able to get the CategoryDTO object from the SelectedItem property. I have attempted various solutions using ItemsSource, DisplayMemberPath and SelectedValuePath. But have only accomplished this <a href="https://i.stack.imgur.com/vUxJ6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vUxJ6.png" alt="Picture of the combobox"></a><br>
  As is seen it displays the colors, but only displays the Name of the selected CategoryDTO and i havent even tested if SelectedItem works correctly yet.
  Below i will put the code i use.</p>
  
  <pre><code>[Serializable]
  public class CategoryDTO
  {
      public string Name { get; set; }
      ...not important...
  }
  
  
  CategoryDTO[] categories = await _isd.GetCategoriesAsync();
  comboBoxCategory.ItemsSource = categories.Select(c =&gt; new CategoryComboBoxItem(c)).ToList();
  comboBoxCategory.DisplayMemberPath = "Name";
  comboBoxCategory.SelectedValuePath = "Name";
  
  public class CategoryComboBoxItem : ComboBoxItem
  {
      public CategoryComboBoxItem(CategoryDTO category)
      {
          this.Background = new SolidColorBrush(category.Color);
          this.Content = category;
      }
  }
  </code></pre>
  
  <p>I dont have anything special specified in the .xaml so i will leave that part out. Beyond this i would like to be able to set the SelectedItem using the Name property. I very much prefer the answers to be in code-behind, but if it is stupidly complicated .xaml only answers are just as fine. I dont have any experience with MVVM, i can assume it will be suggested. I will of course expand my knowledge on that matter as i delve deeper into WPF, but right now i just would like this to work.<br>
  <strong>This is not homework</strong>  </p>
  
  <p><strong>EDIT: forgot to list errors i also get</strong></p>
  
  <blockquote>
    <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''.<br>
    BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'CategoryComboBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''.<br>
    BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'CategoryComboBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
    System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='CategoryComboBoxItem'  </p>
  </blockquote>