Adding one static value to bound ItemSource in XAML

Hi,

I have WPF program which uses basic binding when attaching items to a ComboBox. Data to this combo box is retrieved from server using a WCF service.

Is there a way to add "<select item>" value to source list at the client side?

Do I have to change how data is bound to be able to add "<select item>" as a default value to the selection list?

Could you please provide sample code?
<ComboBox x:Name="ComboBoxBranch"
          ItemsSource="{Binding Source={x:Static Application.Current}, Path=Branches}"
          SelectedItem="{Binding Branch}" />

Open in new window

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.

lucky_jamesCommented:
>>Is there a way to add "<select item>" value to source list at the client side?

you can change the model ( the code behind where the data for the combo is being fetched) and add one more record to it. Then you can fire the event INotifyPropertyChanged.

Check out:
http://odetocode.com/Articles/740.aspx


>>Do I have to change how data is bound to be able to add "<select item>" as a default value to the selection list?

I feel, no. You should provide <select branch> in first place, followed by the branch list. So, selected item index can be put as 0 for the first time when the control loads.


>>Could you please provide sample code?
check out the link i have posted above, espacially "Binding To Collections" and   "Detecting Changes" topics. Of course then try doing the above trick. It should help you.

James

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
Joni KettunenSenior Software EngineerAuthor Commented:
I'll look into those articles and get back after some testing.
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-8fe63272a7f4

Only way to go around this restriction is to use some kind of wrapper which would modify the data source. In a ComboBox this isn't a problem because it doesn't update data directly back to database.

I can't find a way to wrap myself around that binding. I'd like to have some wrapper like

ItemsSource="{Binding  Source=AddSelectItemWrapper={{x:Static Application.Current}, Path=Branches}}"

or something like that. =)
0
Joni KettunenSenior Software EngineerAuthor Commented:
Without code sample it required lot of work to find out the solution. However, suggested article was so well written that reading it was just a pleasure.

Still, the article didn't mention particular problem with ComboBox, you can't update its contents after binding. You have to change contents of source model and bind it again.
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.