Click here to Skip to main content
15,886,578 members
Articles / Programming Languages / C#

Cascading Combo Box Data Using RIA in Silverlight 4

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Apr 2011CPOL 10.5K  
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.

C#
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Microsoft
India India
Nothing special .. I like challenges and love my critics.

Microsoft | Bangalore | India

Blog : http://manaspatnaik.com/blog

Twitter@manas_patnaik

Comments and Discussions

 
-- There are no messages in this forum --