Csla data provider and passing parameter to constructor for combobox loading

Csla data provider and passing parameter to constructor for combobox loading

Old forum URL: forums.lhotka.net/forums/t/8204.aspx


tiptop posted on Wednesday, December 23, 2009

Hello

I need a bit of help with syntax passing a parameter to a constructor.

I am designing a cascading combobox system where combobox 1 has a list of locations and when a user selects one, they are presented with a sub-set of child locations in a second combobox.

I have got each of the comboboxes loading independently using a Csla data provider and uses the GetList method for populating the combobox.

What I can't work out is how to pass a parameter id of the first combobox when a user selects an item to the second combobox.

Do I use Objectdataprovider for this? Or does Csla provide something?

Sample code would be appreciated. I am developing with WPF.

Thanks

tiptop replied on Thursday, December 24, 2009

Any hints or pointers to a section in the book would be appreciated.

ajj3085 replied on Tuesday, December 29, 2009

Don't think I've done this in Wpf, but couldn't you have the binding of the second combobox setup to be the collection property of the first combobox's selected value? 

Otherwise the only thing I can think of is reacting to the first combobox's selecteditemchanged event and having code to fire off a referesh for the second combo's dataprovider.

tiptop replied on Thursday, January 07, 2010

Thanks for the reply ajj3085. Your suggestion was a hint in the right direction.

Now that I have finally got it working, I will share my solution with any other users who maybe struggling to perform such a simple task in WPF.

Initial requirement : I had a form that had two comboboxes. One combobox was a child of the other. Let's call the business object that the form is bound to "Address" and the parent combobox as "Country" and child combobox as "State".

All I wanted to do was to select a country from the first combobox which would then fire off an event to populate the "State" combobox with the appropriate values using XAML and CSLA ( not the traditional Win Forms onchanged event and code behind logic to load the State combobox.)

My first solution, and hence the request for help in this thread, was to create two csla data providers for the CountryList and StateList. These data providers were exactly the same as the RoleList in the Project Tracker example. What I thought I could do was to pass the CountryId as a parameter to the StateList data provider class when the Country combobox was changed. But I couldn't work out the syntax of how to do this through XAML ( this is 75% of the problem when working with WPF is finding out what is possible through XAML and getting the syntax right.).

After a lot of reading around and looking at examples, I finally came across this example,

http://weblogs.asp.net/manishdalal/archive/2008/10/22/cascading-combobox.aspx

The functionality was exactly what I needed but the object structure was not the same. But it was enough to get me started in the right direction.

Therefore, my solution was as follows :

The "Address" object class, I implemented the INotifyChanged interface :

partial class Address : INotifyPropertyChanged


In the "Address" object class, I created a property variable for "StateList" that simply returned the "StateList" NameValueListBase class ( as used by "RoleList" ).

private static PropertyInfo StateListProperty = RegisterProperty(c => c.StateList);
private StateList _StateList= StateListProperty.DefaultValue;
public StateList StateList
{
get { return StateList.GetList(CountryId); }
}

Then in the property set of the CountryId property, I raised a RaisePropertyChanged("StateList") event.

private static PropertyInfo CountryIdProperty = RegisterProperty(c => c.CountryId);
private int _CountryId = CountryIdProperty.DefaultValue;
public int CountryId
{
get { return GetProperty(CountryIdProperty, _CountryId); }
set {
SetProperty(CountryIdProperty, ref _CountryId, value);
RaisePropertyChanged("StateList");
}
}

Then in the XAML for the form, I used a csla dataprovider called "CountryList" to populate the Country combobox.

And I then used the "StateList" variable to populate the "State" combobox when the Country combobox changed. The XAML for this is :

ComboBox x:Name="cboStateList" Grid.Column="1"
Grid.Row="5"
ItemsSource="{Binding Path=StateList}"
DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding Path=StateId}"
/>

That's it. After a lot of tearing my hair out, it turned out to be relatively straight forward solution, with absolutely no code-behind code needed.

Hopefully this will help someone wanting to do something similar in their application.

Copyright (c) Marimer LLC