1

I have a combobox binded to an observable collection through

cmbBladesTab1.ItemsSource = easyRunData.olstBlades;

that works fine. I want the combobox to be binded to all that values plus one.

E.g. easyRunData.olstBlades; contains "PL1", "PL2", "PL3", "PL4"

while cmbBladesTab1 contains "ALL BLADES", "PL1", "PL2", "PL3", "PL4"

--ADD all work has to be done from code-behind

Thanks for your help.

Patrick
  • 3,073
  • 2
  • 22
  • 60
  • 1
    Could you give further details regarding your question? To be honest I didn't understand what exactly are you trying to achieve – Robert J. Dec 16 '15 at 11:55
  • Possible duplicate of [Adding predefined item to a ComboBox with ItemsSource](http://stackoverflow.com/questions/13542072/adding-predefined-item-to-a-combobox-with-itemssource) – Jose Dec 16 '15 at 12:00
  • Check this post maybe it helps you > http://stackoverflow.com/questions/5134152/add-an-item-to-combobox-before-bind-data-from-data-base – Mircea Movila Dec 16 '15 at 12:00

2 Answers2

1

Easiest way would be to add an extra item in the observable collection with some prefixed text / key. That way, because it's in the collection, it will be visible in the combobox and when the user selects this item you can evaluate it to see if it's the added item or not.

A good example is indeed given as an answer on this question add an item to combobox before bind data from data base

Community
  • 1
  • 1
Depechie
  • 6,102
  • 24
  • 46
  • That is the easiest way and works fine. But since I have to do other operations with the observable collection, I'd prefer not to have to change it since it would require to handle everywhere the extra value ("ALL BLADES") – Patrick Dec 16 '15 at 12:32
  • Well because you are directly setting the collection as the its source there is no real other way. You could try to add the item to the item source of the combo box after you assigned the collection to it. – Depechie Dec 16 '15 at 12:34
1

You could add a property, that adds the particular item to the list.

ObservableCollection<string> myCollection;
ObservableCollection<string> MyCollectionViewProp
{
    get
    {
        var tempCollection = new ObservableCollection<string>(myCollection);
        tempCollection.Add("Extra element");
        return tempCollection;
    }
}

Depending on the size of the collection and the number of times it is accessed, this is probably the programmatically simplest solution. If you need to access it often, the worse this solution gets, as it creates a new collection every time. In this case you should probably listen to the CollectionChanged event and keep a separate redundant list.

srandppl
  • 571
  • 4
  • 14
  • @Florian Proksch that might work! But can that collection be serialized? And then I would have to handle both collection, one with extra element only for the combobox and another for all other purposes – Patrick Dec 16 '15 at 12:36
  • As long as your myCollection is serializable, of course. Use the property just for the binding and do whatever you want to do when your extra item is selected in the setter of the property you bound to the SelectedItem of the combo. To be clear: you probably want to serialize myCollection, not the property MyCollectionViewProp. – srandppl Dec 16 '15 at 12:39