=========================================================================================
Update: 10th August 2010; Sorry, folks, this continues to be as complex before. What follows works ~95% of the time but will occasionally fail because the lookup data source is not available when the main data source is queried.
This really should not be difficult, but it appears that it really is.
I think that the solution is to move to an MVVM model, where the ViewModel presents the primary data source as well as the lookup data sources. Better; it provides dependency linking so that when the lookup lists change, then dependant views are updated.
You may now be thinking "WHAT!? It can't be that complicated, surely?!?!?!?". Well, I thought the same, and it seems that it really is that complex
It's all to do with two 21st century concepts;
- Asynchronous (late) binding; As we display the UI, we don't know when all the data will be loaded from the database. What happens if the core dataset (Employees) is loaded before dependant datasets (Employee Position)?
- Multi-threaded / multi-user / multi-context updates; What if we are quite happily wandering around our employee edit screen and somebody else adds a new Employee Position record? How do we reflect that into our (fairly single user) model of the world?
Unfortunately, and I really looked for shortcuts, these are enterprise-level discussions.
I believe that the answer is the MVVM model and I'm currently trying to catch up on my learning, through the following resources;
- MVVM on Wikipedia
- Karl Shifflett on MVVM in WPF
- The excellent Silverlight In Action book by Pete Brown (not yet released)
- The Cinch MVVM framework from the inestimable Sasha Barber
Until I catch up, then I can only recommend the rest of this article as an educational exercise. It ain't much cop for real life.
Sorry!
Scott
=========================================================================================
OK, I figured it out. How to present a data-bound combobox with Silverlight 4. This isn't going to work with SL2 or SL3 because they are missing the SelectedValuePath property.
So, let's set some context. I have a CredentialsType which, for the sake of this example, contains types which are;
- SNMP
- Windows
- SQL Server
- …etc…
Then, I have a CredentialsSet, which has a variety of login and password identifiers. So, for a Windows set, we have;
- CredentialsTypeId = the id of the CredentialsType we mentioned above, that is set to a CredentialsType where the CredentialsType.Name = "Windows"
- DomainColumn = the Active Directory domain name.
- LoginColumn = AD username
- PasswordColumn = AD password
Another CredentialsSet for an SNMP set may look like this;
- CredentialsTypeId = this id of the CredentialsType whose CredentialsType.Name = "SNMP"
- LoginColumn = SNMP Community string
So, the combobox is going to be on the CredentialsSet entity, pointing at the master (CredentialsType) record.
.png)
So, we have a master record for CredentialsType (primary key = CredentialsTypeId, Friendly Name = Name)
And the child record (in other words, the form that is displaying the record that is to be edited, that has a drop down relating to the parent record) has a number of child columns.
The XAML ends up looking like this;
<ComboBox Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="0,3,0,0"
Name="comboBox1" VerticalAlignment="Top" Width="120"
ItemsSource="{Binding ElementName=credentialsTypeDomainDataSource, Path=Data}"
DisplayMemberPath="Name"
SelectedValue="{Binding ElementName=credentialsSetDomainDataSource, Path=Data.CurrentItem.CredentialsTypeId, Mode=TwoWay}"
SelectedValuePath="CredentialsTypeId" />
Here, you'll see that;
- the ItemSource = the path to the data source that holds the complete list of elements (in our case, the CredentialTypes)
- DisplayMemberPath = The name of the property that will be displayed in the dropdown
- SelectedValue = the two way binding (i.e. ID) that links the child record to the parent record
- SelectedValuePath = this should be the same as the SelectedValue and points the caller to the correct value in the parent record

Hi Scott,
Where do you keep the source code for the articles?
Have you got the source code for the "Silverlight 4 and data-bound combobox – the answer" article?
Cheers
C
Scott,
Thanks for writing this up – I'm toying with a Silverlight/RIA Services app and coming from the world of ASP.NET I'm having to learn a lot of things new again.
@Claudio; good question. I haven't posted any source but it seems as if it would be helpful to do so. I'll try to do this, this weekend.
@Orion; no probs. I came from the Windows Foms world and pretty much bypassed ASP.NET, however I understand where you're coming from. There's tons of fantastic stuff in the RIA Services / Silverlight world (if only I was a creative designer!!) but then a small set of what-you-would-think-would-be-basic-stuff, such as comboboxes, are just a nightmare.
Thanks for reading, and 3 x thanks for responding
Scott
Very clean explication. I just try it and it's working very well (outside my DataForm). But If I put my combobox inside my DataForm it's not working :
<toolkit:DataField Grid.Row="4" Grid.Column="0" HorizontalAlignment="Left" >
<ComboBox SelectedValue="{Binding ElementName=ficheespaceclosDomainDataSource, Path=Data.CurrentItem.NoCategorieEspaceClos, Mode=TwoWay}" Width="200" Height="40" SelectedValuePath="NoCategorieEspaceClos" ItemsSource="{Binding Source={StaticResource categoriesespacesclosDomainDataSource}, Path=Data}" DisplayMemberPath="DescriptionCategorieEspaceClos_fr" />
</toolkit:DataField>
What's can I do for that?
THank you a lot Scott again
Hi Louis
I suspect that I may have been premature – adding a combobox continues to be a nightmare, even in my own project. Please let me have a couple of days and I'll post a fuller response.
Regards
Scott
Any updates on this regarding source_code?
I find it really hard to just make to simple comboboxes dependant on each other in a dataform..
Easiest way to bound data from DB table with population to combobox is to put DomainDataSource into dataform here is sample (SL 4) i write only what need to put in datafield an ria must be NOT in datafield, just make sure you pit it inside dataform. No need aditional coding in C#
<toolkit:DataField Label="Customer">
<ComboBox Height="23" ItemsSource="{Binding ElementName=customersTabDomainDataSource, Path=Data}" Name="customersTabComboBox" Width="120" DisplayMemberPath="ShortOrgName">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</toolkit:DataField>
<riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:CustomersTab, CreateList=true}" Height="0" LoadedData="customersTabDomainDataSource_LoadedData" x:Name="customersTabDomainDataSource" QueryName="GetCustomersTabsQuery" Width="0">
<riaControls:DomainDataSource.DomainContext>
<my:CustomersDomainContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>