Skip to main content

Microsoft Silverlight

Answered Question Databinding to ComboBoxRSS Feed

(0)

M.S
M.S

Member

Member

22 points

65 Posts

Databinding to ComboBox

 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.

 

 

 


M.S

jay nanavati
jay nana...

Contributor

Contributor

3388 points

624 Posts

Re: Databinding to ComboBox

 See here. It is good article about ComboBox in Silverlight:

http://www.silverlightshow.net/items/Silverlight-ComboBox.aspx

Jay K Nanavaty
www.technologyopinion.com
Mark as answer if it helps. It will also help others...

Kaushlendra.Singh
Kaushlen...

Member

Member

150 points

33 Posts

Answered Question

Re: Databinding to ComboBox

Hi!

The implementation of your requirement is quite simple.

1. Set the Itemsource of your combobox with collection. I am taking a key value pair in this case.
List<KeyValuePair<int, string>> userStatus = new List<KeyValuePair<int, string>>();
userStatus.Add(
new KeyValuePair<int,string>(1, "Good-Standing"));
userStatus.Add(
new KeyValuePair<int,string>(2, "Flagged"));
userStatus.Add(
new KeyValuePair<int,string>(3, "On-HOld"));

ddlCombo.ItemsSource = userStatus;
ddlCombo.SelectionChanged +=
new SelectionChangedEventHandler(ddlCombo_SelectionChanged);

2. Type caste the selected item into your datatype to get the desired data 
void ddlCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        txtBox.Text = ((
KeyValuePair<int, string>)ddlCombo.SelectedItem).Key.ToString();
}

I hope this answers your query Smile

Kaushlendra Singh
Software Engineer
GrapeCity India

PS. Please "Mark as Answer" if this post answered your question. :)

M.S
M.S

Member

Member

22 points

65 Posts

Re: Databinding to ComboBox

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

M.S

Kaushlendra.Singh
Kaushlen...

Member

Member

150 points

33 Posts

Re: Databinding to ComboBox

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.

Smile 

Kaushlendra Singh
Software Engineer
GrapeCity India

PS. Please "Mark as Answer" if this post answered your question. :)
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities