Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Databinding to ComboBox
4 replies. Latest Post by Kaushlendra.Singh on July 30, 2009.
(0)
M.S
Member
22 points
65 Posts
07-30-2009 9:30 AM |
Hello All,
I'm having trouble getting databinding to a ComboBox to work correctly. So I was wondering if anyone can tell me how to get it working and/or if anyone has used some of the third-party control maker's ComboBox and if it works well.
Here's my situation.
I have a main Customers table. Each customer can have one of three different statuses. So I have a Status field in the Customers table, with either 1, 2, or 3, and a related table like:
CustStatus
ID Desecription
1 Good-Standing
2 Flagged
3 on-hold
I related the Customers Status field to the CustStatus ID.
I can databind the ComboBox to the CustStatus table and display the descriptions in the ComboBox list. But I can't get it to know which item is selected based on the Status field of the selected Customer.
jay nana...
Contributor
3388 points
624 Posts
07-30-2009 9:40 AM |
See here. It is good article about ComboBox in Silverlight:
http://www.silverlightshow.net/items/Silverlight-ComboBox.aspx
Kaushlen...
150 points
33 Posts
07-30-2009 1:48 PM |
Hi!
The implementation of your requirement is quite simple.
ddlCombo.ItemsSource = userStatus;ddlCombo.SelectionChanged +=
I hope this answers your query
07-30-2009 5:54 PM |
Thank you. I modified it slightly because the KeyValuePair didn't display well in the ComboBox.
Here' what I ended up dong:
private void cmbStatus_Loaded(object sender, RoutedEventArgs e) { List<Status> custStatus = new List<Status>(); custStatus.Add(new Status(1,"In Good-Standing")); custStatus.Add(new Status(2, "Flagged")); custStatus.Add(new Status(3, "On-Hold")); cmbStatus.ItemsSource = custStatus; } private void cmbStatus_SelectionChanged(object sender, SelectionChangedEventArgs e) { Customer currentCustomer = listCustomers.SelectedItem as Customer; currentCustomer.Suspended = ((Status)cmbStatus.SelectedItem).ID; }
07-30-2009 11:17 PM |
Great! Another way to bind your combo box can be through xaml by binding the ItemSource.
To get the selected Item also you can use an IValueConverter. This will enable you to have zero code behind.