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?
 
lucky_jamesConnect With a Mentor Commented:
>>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
 
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
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.