views:

81

answers:

1

I have a listbox and I want its ItemsSource to be all the items of an ObservableCollection + an additional static text such as "All Items."

Right now I just have something similar to:

listbox1.ItemsSource = from Car c in Cars
                       select c.Model

I know I could just manually add the text to the listbox, but I want it to be part of the query because the linq query is bound using the Obtics library (so the UI updates reactively). I am not too familiar with linq queries outside the basics, so does anyone know if this is possible? Thanks

+1  A: 

You can use the Union() operator to add more objects into your selected set.

string[] additionalItems = {"All Items"};

listbox1.ItemsSource = (from Car c in Cars
                       select c.Model)
                       .Union(additionalItems);
womp
In the Union clause, `from s in additionalItems select s` is redundant: just `additionalItems` has the same effect and is more concise.
itowlson
Yeah I just realized my initial example was poor. Was editing it as you commented ;)
womp
Thanks guys. Works perfect.
AdamD