Individual values ​​in WPF Combobox

I would like to get individual values ​​in my combo box

as an example, the meanings are: blue, blue, yellow, red, orange

I would like it to just display blue.

My main idea was to get all the combo box values ​​into an array, set the array as separate, and then re-populate the combo box. Is there another way?

If I didn’t get all the values ​​from the combo box?

thank

EDIT - Class:

public class DistinctConverter : IValueConverter
{

}

EDIT - Debugging:

enter image description here

+3
source share
3 answers

You can create IValueConverterone that converts your list into a separate 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 as follows:

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

, List<String> values = blue, blue, yellow, red, orange

ComboBox.ItemsSource = values.Distinct();

MVVM-, boxsource ,

public List<string> values
{
    get
    {
    return value.Distinct();
     }
}
+1

if you are using WPF C # 4.0

List<object> list = new List<object>();
        foreach (object o in myComboBox.Items)
            {
            if (!list.Contains(o))
                {
                list.Add(o);
                }
            }
        myComboBox.Items.Clear();
        myComboBox.ItemsSource=list.ToArray();
0
source

All Articles