本文介绍了WPF 组合框中的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的数据绑定组合框中获得不同的值
I would like to get distinct values in my databound combo box
举个例子,它的值是:blue、blue、yellow、red、orange
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?
谢谢
编辑——类:
public class DistinctConverter : IValueConverter
{
}
编辑——调试:
推荐答案
您可以创建一个 IValueConverter
将您的列表转换为不同的列表:
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();
}
}
将此添加到资源中:
<local:DistinctConverter x:Key="distinctConverter" />
并像这样使用它:
<ComboBox ItemsSource="{Binding Vals, Converter={StaticResource distinctConverter}}" />
这篇关于WPF 组合框中的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!