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 combobox
es: Product
and Version
. Depending on the product selection, the versions should get populated.
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.
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;
}