Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Cascading Combo Box Data Using RIA in Silverlight 4

0.00/5 (No votes)
12 Apr 2011 1  
In this post, we will replicate the cascade combo box loading scenario, where data of other combos depend on the selection in the first.

Having introduced the concept of WCF RIA services with EF and Silverlight in my earlier post, now it's time to take up issues we usually face in day to day coding. In this post, we will replicate the cascade combo box loading scenario, where data of other combos depend on the selection in the first.

Here we have two comboboxes: Product and Version. Depending on the product selection, the versions should get populated.

SNAGHTMLe83527

As usual, we will follow these steps:

  • Bind data to the Product combo
  • On the SelectedIndexChange event of the Product combo, we will bind to the Version combo

Binding Data to the Product Combo Box

As in my earlier post, I have created a Domain Context called myDomainContext using Entity Framework code generation.

image

Using the myDomainContext entity query, we will bind the product data to the combobox.

myDomainContext cont = new myDomainContext(); 
LoadOperation<PRODUCT> prd = cont.Load(cont.GetPRODUCTsQuery()); 
cmbProducts.ItemsSource = prd.Entities;

Now on the selection changed event, you will try to bind to the Version combo.

On Selection Changed

Create a custom EntityQuery based on the combobox selection and load the query using a LoadOperation. All queries from a domain context are executed asynchronously. To execute the query, we pass the EntityQuery object as a parameter to the Load method.

private void cmbProducts_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    myDomainContext cont = new myDomainContext(); 
    ComboBox cmb = (ComboBox)sender; 
    EntityQuery<VERSION> query = from c in cont.GetVERSIONsQuery() 
    where c.VERSION_PRODUCTID == ((PRODUCT)cmb.SelectedItem).PRODUCTID 
    select c; 
    LoadOperation<VERSION> prd = cont.Load(query); 
    cmbVersions.ItemsSource = prd.Entities; 
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here