WPF Combobox中的不同值 [英] Distinct Values in WPF Combobox

查看:193
本文介绍了WPF Combobox中的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



作为一个例子,它的值是:blue,blue,yellow,red, p>

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



我的主要想法是将所有组合框值数组,将数组设置为distinct,然后重新填充组合框。有没有其他方法?



如果没有,我如何从组合框中获取所有的值?



感谢



编辑 - 类:

  public class DistinctConverter: IValueConverter 
{

}

EDIT - 调试: / p>

>

解决方案

您可以创建一个将您的列表转换为不同列表的 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(
对象值,类型targetType,对象参数,CultureInfo文化)
{
throw new NotSupportedException();
}
}

将此资源添加到资源:

 < local:DistinctConverter x:Key =distinctConverter/& 

并使用它:

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


I would like to get distinct values in my databound combo box

as an example the values it has are: blue, blue, yellow, red, orange

I would like it to just display blue once.

My main thought was to get all combo box values into an array, set the array as distinct and then re-populate the combo box. Is there any other way?

If not how would I actually get all the values from the combo box?

Thanks

EDIT -- Class:

public class DistinctConverter : IValueConverter
{

}

EDIT -- Debug:

解决方案

You could create an IValueConverter that converts your list into a distinct list:

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();
    }
}

add this to resources:

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

and use it like this:

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

这篇关于WPF Combobox中的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆