How to add "<select option>" item into WPF ListBox when data is delivered through EntityFramework?

The question is to find simple, yet reusable code to do following:

How do I add "<select option>" item into ListBox choises. It should appear as a first item in ListBox and rest of the data should appear after that (preferably in alphabetical order). Is there a neat way to do this?

Should I use a UserControl or can this new kind of empty selection item titled "<select option>" coded in with few lines of code?

Currently I am developing a WPF application where we have half dozen developers fiddling with UI. Of course each one has their own way to implement this. I'd need good way to replace all those ridiculous "unique" solutions to keep code maintainable.

So, I am looking for a simple but elegant solution.
Joni KettunenSenior Software EngineerAsked:
Who is Participating?
Joni KettunenSenior Software EngineerAuthor Commented:
I've tried some code samples and found out through search in web that:

"When the ComboBox control binds to DataSource, you  cannot update Items of it."'ve tried some code samples and found out through search in web that:

"When  the ComboBox control binds to DataSource, you  cannot update Items of  it."

Problem is that I'm getting data straight from DataContext at XAML page. Currently there is no code behind (C# code) which binds source to the ListBox.
Just add it to the datasource you are using for binding.
Joni KettunenSenior Software EngineerAuthor Commented:
With some sample code I'd be happy to give good grade to a answer.

Another detail which I forgot to mention is that most of the time contents of a list is populated through Entity Framework entities.

Meaning code which populates ListBox is located in xaml and contents are populated through existing list.

ItemsSource="{Binding Source={x:Static Application.Current}, Path=SupplierNames}"
You should be adding "<Select Option>" to the SupplierNames and set the binding as twoway

ItemsSource="{Binding Source={x:Static Application.Current}, Path=SupplierNames, Mode=TwoWay}"

Then it would work
Joni KettunenSenior Software EngineerAuthor Commented:
Actually Mode doesn't matter. It just states if data is saved to database or not. TwoWay mode would like to save data to database, but I don't want to save that "select" option into database.

Anyway, thank you for your feedback even tough it didn't lead to results I needed. I ended up adding one wrapper layer. Now combobox bound data automatically has an extra empty value.

Codes to working solution can be found from  these answers:
- Data source can't be updated after binding, but binding it to modified source might be the key:

- How to insert data into datasource:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.