Distinct Values in WPF Combobox

后端 未结 3 2061
时光取名叫无心
时光取名叫无心 2020-12-21 07:33

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 di

3条回答
  •  独厮守ぢ
    2020-12-21 08:02

    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().Distinct();
        }
    
        public object ConvertBack(
            object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    
    
    

    add this to resources:

    
    

    and use it like this:

    
    

    提交回复
    热议问题