EDIT: The issue underneath is fixed, GO TO EDIT2 in this post.
I have an Organisation entity and a Region entity. An object of type Organisation can have one or more Region objects connected to it, thus I have a foreign key in my Region entity to the Organisation Entity. The Organisation and Region objects are pulled from my database using WCF RIA and entity framework. I want to put the Organisation objects in one ComboBox and the Region objects in another ComboBox, and when selecting an organsation having the ComboBox for Region objects automatically only showing regions that are connected to the selected organisation. Should be pretty basic, but the way I’ve designed it right now it doesnt work at all.
So, any hint to how I can achive this? A simple simple codeexample is much appreciated!
(I’m using SL4,WCF RIA MVVM)
EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2 EDIT2:
Using Venomo’s ElemntBinding answer this is working great for me now when I want to add a new object to my collection, and I’m just pulling the avaible countries and connected regions and then type a site in a textbox…So I get my combination of Organisation, region and site in my database 🙂
Now, I’ve got a new problem when I want to EDIT a site in my collection. In EDIT mode, I want the two dropdowns to be preselected and disabled (BusinessRule is that I can edit the sitename, not which organisation og region it’s connected to). So by setting the SelectedIndex property on Organisation combobox I get my organisation selected, but when doing the same on the Regions combobox it fails with an Object Reference error.
You can achieve this with some clever
ElementBindings.Basic example:
Let’s say we have a simple class like this:
Then, we’ll have two
ComboBoxes: one for choosing a country and another for choosing a region in that country. The second one should update itself when the value of the first one changes.Okay, first we have to tell Silverlight how to display a
Country. For complex scenarios we could use aDataTemplatefor that, but for now, we will only need theDisplayMemberPathproperty of the ComboBox class.So, we create a simple collection of these objects in the code behind:
I know that those are not all of the regions in the example countries, but this is a Silverlight example and not a geographical lesson.
Now, we have to set the
ItemsSourceof theComboBoxto this collection.Both of these can be in the constructor in the code-behind.
Okay, now back to XAML!
We’ll need another ComboBox and a way for telling it to get its items from the other collection dynamically.
Binding its
ItemsSourceto the otherComboBox‘s selected item is just the most obvious way to achieve that.This should do the trick quite simply.
If you use MVVM:
You can bind to the
ItemsSourceof the firstComboBoxfrom theViewModel. The rest remains the same.To tell what the selected values are to the
ViewModel, use Two-way bindings on theSelectedItemproperty of bothComboBoxes and bind that to whatever property you have for it in theViewModel.If your collection can change dynamically:
If the list of the countries (or whatever it is that you want to use this for) can change during runtime, it is best if you implement
INotifyPropertyChangedfor theCountryclass and for the regions, useObservableCollection<>.If it doesn’t need to change during runtime, no need to bother with these.