微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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

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

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

谢谢

编辑 – 班级:

public class distinctConverter : IValueConverter
{

}

编辑 – 调试:

解决方法:

您可以创建一个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}}" />

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐