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

Luq*_*sen 1 c# data-binding wpf combobox code-behind

我读过其他几篇文章,但没有人能够回答我的问题组合
我有一个 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在 .xaml 中没有指定任何特殊内容,因此我将忽略该部分。除此之外,我希望能够使用 Name 属性设置 SelectedItem。我非常喜欢将答案放在代码隐藏中,但如果它非常复杂,则仅使用 .xaml 答案就可以了。我对 MVVM 没有任何经验,我可以假设它会被建议。当我深入研究 WPF 时,我当然会扩展我在这方面的知识,但现在我只想让它发挥作用。
这不是家庭作业

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

System.Windows.Data 错误:4:无法找到引用“RelativeSource FindAncestor、AncestorType='System.Windows.Controls.ItemsControl”、AncestorLevel='1'' 进行绑定的源。
BindingExpression:Path=Horizo​​ntalContentAlignment; 数据项=空;目标元素是“CategoryComboBoxItem”(名称=“”);目标属性是“Horizo​​ntalContentAlignment”(类型“Horizo​​ntalAlignment”)System.Windows.Data 错误:4:无法找到与引用“RelativeSource FindAncestor、AncestorType='System.Windows.Controls.ItemsControl”、AncestorLevel='1'' 进行绑定的源。
BindingExpression:Path=VerticalContentAlignment; 数据项=空;目标元素是“CategoryComboBoxItem”(名称=“”);目标属性是“VerticalContentAlignment”(类型“VerticalAlignment”)System.Windows.Data 错误:26:对于已属于 ItemsControl 容器类型的项目,将忽略 ItemTemplate 和 ItemTemplateSelector;类型='类别组合框项目'

Rac*_*hel 5

要使用 WPF 正确执行此操作,我认为您需要更好地了解 WPFDataContext及其工作原理。我写了一篇博客文章只是为了在 SE 上链接:你所说的“DataContext”是什么?DataContext我强烈建议您在使用 WPF 进行任何操作之前先确保您了解这一点。

您的总体想法是您想要将 a 绑定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>
Run Code Online (Sandbox Code Playgroud)

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

<ComboBox ItemsSource="{Binding CategoryList}" ..>
Run Code Online (Sandbox Code Playgroud)

或者后面有代码

MyComboBox.ItemsSource = CategoryList;
Run Code Online (Sandbox Code Playgroud)

ComboBox.SelectedItem这也将使您与列表中选定的项目同步CategoryDTO,因此您可以直接投射它来执行某些操作

CategoryDTO selected = (CategoryDTO)MyComboBox.SelectedItem;
DoSomethingWithSelected(selected);
Run Code Online (Sandbox Code Playgroud)

或绑定它,以便可以轻松地从 DataContext 使用

<ComboBox SelectedItem="{Binding SelectedCategory}" ..>
Run Code Online (Sandbox Code Playgroud)
<ComboBox ItemsSource="{Binding CategoryList}" ..>
Run Code Online (Sandbox Code Playgroud)

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