Hille
Hille

Reputation: 2319

Ignore empty ComboBoxItems

I have a ComboBox which has bounded items, like

<ComboBox ItemsSource="{Binding Path=list}"
          DisplayMemberPath="name"
          SelectedValuePath="id"
          SelectedValue="{Binding Path=id, Mode=TwoWay}"/>

The Problem is that there is a minimum of 10 items in the list which can be empty.

Is there a way to ignore empty values as items?


I tried to remove the empty values with different methods, like

[System.Xml.Serialization.XmlIgnore]
public ObservableCollection<myclass> list
{
    get
    {
        ObservableCollection<myclass> list = new ObservableCollection<myclass>();

        foreach (var item in org_list.Where(x => !string.IsNullOrWhiteSpace(x.name)).ToList())
        {
            list.Add(item);
        }

        return list;
    }
}

but it doesn't update the ComboBox any more if I'm adding any items to org_list.

Upvotes: 0

Views: 65

Answers (1)

ΩmegaMan
ΩmegaMan

Reputation: 31721

Create a mirror list which exposes the list without the empties. For changes subscribe to the observable collections events that are relevant such as add/remove and keep the mirror consistent. Then bind to this filtered list.

Upvotes: 1

Related Questions