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.
LVL 4
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."
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/4d4462ba-f4d7-4f1d-8747-8fe63272a7f4I'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."
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/4d4462ba-f4d7-4f1d-8747-8fe63272a7f4

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.
0
 
SriVaddadiCommented:
Just add it to the datasource you are using for binding.
0
 
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}"
0
 
SriVaddadiCommented:
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
0
 
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:
http://www.experts-exchange.com/Microsoft/Development/Microsoft_Programming/WPF_and_Silverlight/Q_26074250.html

- How to insert data into datasource:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26147721.html



0
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.