c# - WPF 组合框中的不同值

标签 c# wpf silverlight combobox distinct

我想在我的数据绑定(bind)组合框中获得不同的值

例如,它的值是:蓝色、蓝色、黄色、红色、橙色

我希望它只显示一次蓝色。

我的主要想法是将所有组合框值放入一个数组中,将数组设置为不同的,然后重新填充组合框。还有其他办法吗?

如果不是,我实际上如何从组合框中获取所有值?

谢谢

编辑——类:

public class DistinctConverter : IValueConverter
{

}

编辑——调试:

enter image description here

最佳答案

您可以创建一个 IValueConverter 将您的列表转换为不同的列表:

public class DistinctConverter : IValueConverter
{
    public object Convert(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        var values = value as IEnumerable;
        if (values == null)
            return null;
        return values.Cast<object>().Distinct();
    }

    public object ConvertBack(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

将其添加到资源中:

<local:DistinctConverter x:Key="distinctConverter" />

并像这样使用它:

<ComboBox ItemsSource="{Binding Vals, Converter={StaticResource distinctConverter}}" />

关于c# - WPF 组合框中的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5995989/

相关文章:

c# - C#中如何在非静态方法中调用静态方法

c# - 如何在可视化树中查找元素? wp7

c# - 不接收来自 BeginGetResponse 的回调

silverlight - Windows Phone 7-是否可以在Silverlight中使用其相机?

c# - WPF 中的分辨率独立性如何工作?

C# 下拉项目 0 - 100 而不添加每个项目?

c# - lambda Select 表达式中的 AddRange/concat 功能

wpf - 绑定(bind)到 ViewModel.SubClass.Property(子属性)

c# - wpf Button.MouseLeftButtonDown 根本不起作用

wpf - 当组合框位于 ListView 单元格中时如何通过委托(delegate)命令处理 SelectionChanged 事件