Java组合框的颜色_WPF数据绑定组合框中的彩色项目

I have read a couple of other articles, but none have been able to answer my combination of issues

我有一个ComboBox,我想在其中显示不同颜色的项目,这可以通过使用ComboBoxItem并设置其背景来完成 . 当我想以不同的颜色存储我的CategoryDTO并稍后能够再次提取它时,我的问题出现了 . 我需要显示的只是我的CategoryDTOs的颜色和Name属性 . 然后我必须能够从SelectedItem属性中获取CategoryDTO对象 . 我使用ItemsSource,DisplayMemberPath和SelectedValuePath尝试了各种解决方案 . 但是只完成了这个

vUxJ6.png

如图所示它显示颜色,但只显示所选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中指定任何特殊内容,所以我会把那部分留下来 . 除此之外,我希望能够使用Name属性设置SelectedItem . 我非常希望答案是代码隐藏的,但如果它是愚蠢的复杂.xaml只有答案也一样好 . 我对MVVM没有任何经验,我可以假设它会被建议 . 当我深入研究WPF时,我当然会扩展我对这个问题的了解,但是现在我希望这可以工作 .

This is not homework

EDIT: forgot to list errors i also get

System.Windows.Data错误:4:无法找到绑定源,引用'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1'' . BindingExpression:路径= HorizontalContentAlignment;的DataItem = NULL; target元素是'CategoryComboBoxItem'(Name =''); target属性为'HorizontalContentAlignment'(类型'HorizontalAlignment')System.Windows.Data错误:4:无法找到绑定源,引用'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1'' . BindingExpression:路径= VerticalContentAlignment;的DataItem = NULL; target元素是'CategoryComboBoxItem'(Name ='');目标属性是'VerticalContentAlignment'(类型'VerticalAlignment')System.Windows.Data错误:26:ItemConmplate和ItemTemplateSelector被ItemsControl的容器类型的项目忽略;类型= 'CategoryComboBoxItem'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值