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.
Francis,
I’m finding this sample difficult to work with as I’m not very familiar with a few things like VB, Linq, RIA, etc. So I’m creating a simplified version in c# which emulates something more similar to how I would be using it with a few assumptions:
· The developer already has a way to fetch the data from the server and manipulating (filtering) the data will all be done client side.
· The list of states and counties can be persisted in static collections client side.
I got the converters to work only part way as I used code from the sample project “XamWebGrid_Testing” from Matt which was easier for me to understand, but they don’t do all the work needed.
A problem in both projects (yours and mine) is when we click into a grid cell for edit, the XamComboEditor does not have the current item selected in its list (no item is selected). The appropriate behavior is to have the current item selected in the list as it would be if we were working in a data entry screen (window).
The problems with my sample are:
1. The XamComboEditors are not writing to the underlying property (probably because I don’t have the converter properly setup)
2. The County dropdown is not being filtered by the state combo’s selection yet. I have a static method that will return a filtered list of counties (CountyHelper. GetCountiesByStateId) we can use if it helps any.
Can you please help me get this sample running correctly? It will probably be good code to offer to others with similar needs. I did a lot of googling and your post is the ONLY one I’ve found regarding using a XamComboEditor in a xamgrid.
Thanks.
Please use this sample project as its a little further along now.
George,
I'll create a private support case with you for better tracking/notification and will also post the resolution here.
Thanks,
Francis
Here’s an updated copy of the project where the State combo is now mostly working. I’m starting to understand the converters better and realized I needed to create a 2nd converter for this combo. One to convert to text for the Text Block and one to convert to int for the combo. Its now writing to the business object, but when I click in a State cell to make the combo show it’s dropdown list, nothing is selected in the list (as it does in your sample). So this is my next challenge before moving on to making the State combo filter the County Combo.
Fyi: I named my state class “StateTest” and the county class “CountyTest” so their names would not conflict with your classes in DataUtil.cs which I was using as reference (ugly names… I know).
Thanks for your help in this!
Thanks for the sample. The stateComboBindingToIntegerConverter is currently returning an integer, while the SelectedItem of the XamComboEditor is expecting a StateTest object.
You can modify your return statement to be this:
return GetStateTestByID(inVal);
rather than this:
return GetStateTestByID(inVal).St_Id;
...and you should see the behavior you expect.
Please let me know if I can be of further assistance.
Thanks Francis that worked great. I thought i had already tried that step. Can you please show me the next step which is to filter the list of counties by the state id? Since I'm not using RIA services and the state and counties lists will be static lists persisted in the client side session, i will need a different technique than the one you demonstrated. I know I can query the counties something like this:
static
public IEnumerable<CountyTest> GetCountiesByStateId(int id)
{
var countiesByState =
from county in _list
where county.Cnt_St_Id == (int)id
select county;
return countiesByState;
}
But I'm not sure how to get this list into the county xamcomboeditor.
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.