组合框中的 WPF 数据绑定彩色项目

WPF databinding colored items in a combobox

我已经阅读了几篇其他文章,但是 none 已经能够回答我的问题组合
我有一个 ComboBox,我想在其中显示不同颜色的项目,这可以通过使用 ComboBoxItem 并设置其背景来完成。当我想以不同的颜色存储我的 CategoryDTO 并且稍后能够再次提取它们时,我的问题就出现了。我需要显示的只是我的 CategoryDTO 的颜色和名称 属性。然后我必须能够从 SelectedItem 属性 中获取 CategoryDTO 对象。我尝试过使用 ItemsSource、DisplayMemberPath 和 SelectedValuePath 的各种解决方案。却只做到了这一点
如图所示,它显示颜色,但只显示所选 CategoryDTO 的名称,我什至还没有测试 SelectedItem 是否正常工作。 下面我将把我使用的代码。

[Serializable]
public class CategoryDTO
{
    public string Name { get; set; }
    ...not important...
}


CategoryDTO[] categories = await _isd.GetCategoriesAsync();
comboBoxCategory.ItemsSource = categories.Select(c => 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;
    }
}

我没有在 .xaml 中指定任何特殊内容,所以我将省略该部分。除此之外,我希望能够使用名称 属性 设置 SelectedItem。我非常喜欢代码隐藏的答案,但如果它非常复杂。xaml 只有答案也一样好。我对 MVVM 没有任何经验,我可以假设它会被建议。当我深入研究 WPF 时,我当然会扩展我在这方面的知识,但现在我只是希望它能起作用。
这不是作业

编辑:忘记列出错误我也得到

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''.
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''.
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'

要使用 WPF 正确执行此操作,我认为您需要更好地了解 DataContext 及其工作原理。我写了一篇博客 post 只是为了在 SE 上链接:What is this "DataContext" you speak of?。我强烈建议您在使用 WPF 进行任何操作之前确保您了解 DataContext

您的总体想法是将 ComboBox 绑定到 CategoryDTO 项列表,并将 SelectedValue 属性 设置为 Name.

<!-- create a ComboBox -->
<ComboBox x:Name="MyComboBox" SelectedValuePath="Name">
    <!-- Add a custom Style to the individual items in combobox -->
    <ComboBox.ItemContainerStyle>
        <!-- In custom style, bind background color -->
        <Style TargetType="{x:Type ComboBoxItem}">
           <Setter Property="Background" Value="{Binding Color}"/>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

如果您的 DataContext 设置正确,您可以使用绑定为 ComboBox 设置项目

<ComboBox ItemsSource="{Binding CategoryList}" ..>

或隐藏代码

MyComboBox.ItemsSource = CategoryList;

这还会将您的 ComboBox.SelectedItem 与列表中选定的 CategoryDTO 项目同步,因此您可以直接对其进行施放以对其进行处理

CategoryDTO selected = (CategoryDTO)MyComboBox.SelectedItem;
DoSomethingWithSelected(selected);

或绑定它以便从 DataContext 中轻松使用

<ComboBox SelectedItem="{Binding SelectedCategory}" ..>
// Can now use SelectedCategory directly
DoSomethingWithSelected(SelectedCategory);

注意:根据 .Color 属性 的数据类型,您可能需要使用 Converter.Color 值转换为 SolidColorBrush.Background 属性。网上应该有很多转换器的例子,或者直接问你是否需要帮助。