WPF Combobox中的不同值 [英] Distinct Values in 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屋!