I'd like to share a useful sample demonstrating XamComboEditors used as cascading filters in a XamGrid in Silverlight 4. The sample was created for a customer by our Developer Support team (kudos primarily to Jason D). The sample is in VB.NET as per the customer's specs, so I hope to have a C# version at a later date.
The sample uses RIA services to provide server-side data access; and in this case the data is coming from a code class in the web project itself, not from an external source, so you should be able to run this sample stand-alone. To do this, the DomainService class returns IQueryable objects based on the ObservableCollections provided by the Data Model.
The XamGrid has related Country, State, and City columns. Selecting a Country causes the State drop-down items to be filtered by Country, and so on.
I hope this sample is a useful guide. Again, I hope to have a C# version soon. Please feel free to post any comments, questions and improvements.
What would differe if data is coming from database
I have done what needs to be done . It still does not show correct data
Francis,Please never mind my last message and attachment to you. I made a new sample solution and cleaned it up by getting rid of the stuff not being used; I refactored it, and renamed things to make it cleaner and easier to follow for people who want to use it in the future. Also, this will make a better reference for me in the future when I need to apply this technique. In the process I have discovered another flaw that needs to be addressed to make this usable in production.
County must be a nullable property. 1) a business app may have a business rule allowing null counties in the db; 2) it must be nullable in the business object because when a user selects a different State from the State combo, the current county is no longer valid and ‘must’ be cleared out.When the county property is NOT nullable, all works OK. But when we make it nullable we are not able to persist the county value when it loses focus. When county is not nullable, we get this exception which you will not notice unless you are watching the output window:
<<<<<System.Windows.Data Error: ConvertBack cannot convert value 'null' (type 'null'). BindingExpression: Path='CountyId' DataItem='XamgridHierarchalComboEditor.RowItem' (HashCode=32406629); target element is 'Infragistics.Controls.Editors.XamComboEditor' (Name=''); target property is 'SelectedItem' (type 'System.Object').. System.InvalidOperationException: Can't convert type null to type System.Int32. at MS.Internal.Data.DynamicValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture) at System.Windows.Data.BindingExpression.UpdateValue().>>>>>
Since State is nullable and it works OK, I changed some of County XAML and County converters to be similar to State, but that had no effect. I also put “System.Diagnostics.Debug.WriteLine(…)” code in the converters to help watch what’s happening. My best guess is that the county ItemSelected event is not working correctly. After reading these comments by Alex http://blogs.infragistics.com/forums/p/33069/180563.aspx, I’m wondering if it’s a synchronization issue with the current item.
I’ve attached the new smaller-cleaned up project to work from. Please let me know how you think we can resolve this.Thanks.
I think we have the County drop-down working close to how you need it in the attached sample. I am using a singleton class for a list of CountyTest (see County.cs) and I had to reverse the order of the ItemSource and SelectedItem properites of the XamComboEditor in XAML (ItemsSource had to be set first).
Very good Francis. The main pieces are now working and all that is left are the tiny things that make it work real nice and smooth for the user. With your help I have the State and Category dropdowns working so when the user clicks on the grid cell, the dropdown automatically opens and the correct item is selected in the list. I also made it so when the user selects a different State, the County is automatically cleared as it’s no longer valid. However, with the different converters used for the county dropdown, I’m not able to make the current county selected in the list as it opens. So here’s what I need help on now:
1) Make the current county the selected item in the list when the dropdown list opens.
2) If there are many items in the list and the current item is down below the visible items, have the list scroll up so the selected item is visible.
At this point I think we’ll have some cascading combos working real slick.
Thanks again for your help.
I modified the settings of the XamComboEditor's SelectedItem and ItemsSource so that they both use a Binding object. The ItemsSource binds to the State ID and gets all the counties through a filtered list provided by your helper method, called by the converter. The SelectedItem is a TwoWay Binding that binds to the County ID.
I hope this works for you. Please check out the attached sample.