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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.