Skip to main content
Home Forums Silverlight Programming WCF RIA Services combobox data binding problem
6 replies. Latest Post by ColinBlair on November 12, 2009.
(0)
kent.zhou
Member
109 points
397 Posts
11-06-2009 1:14 PM |
I have a person table with departmentID column which will make Department is an association member.
Then I create ComboBox as below: (the datacontext for this view is Person, I loaded Person and Department with eager-loading using ria services)
<ComboBox ItemsSource="{Binding Departments}" DisplayMemberPath="DepartmentName" SelectedItem="{Binding Person.Departments}"/>
When I run the app, I can see all departments in the dropdown, but can not set the right value for SelectedItem. when data is binding to the UI, nothing selected in the combobox.
How to resolve this problem?.
Ardman
Contributor
3336 points
922 Posts
11-07-2009 5:34 AM |
You need to create a Loaded event in your code behind. For example:
this.context.Load(this.context.GetPersons(), this.LoadedPersons, null);
You can see a new method called LoadedPersons. Create this a method:
private void LoadedPersons(LoadOperations lo)
{}
Here is where you can set the SelectedItem :o)
11-07-2009 10:35 AM |
I use viewmodel to load data for view. In view model, the property Person.Departments has implemented INotifyPropertyChanged interface.
So when async loading the data on Person has been done, it means Person.Departments has been changed, then it should be reflected in view automatically. Why I need to set it again when async call event has been completed?
ColinBlair
6579 points
1,291 Posts
11-09-2009 9:24 AM |
If the ComboBox ever finds that the bound SelectedItem does not exist in the bound ItemsSource then it breaks the binding on SelectedItem.
Min-Hong...
3375 points
377 Posts
11-11-2009 3:49 AM |
Hi,
The combo box will search through it's items to find a match one with the set-to-be SelectedItem . If it finds no match the selectedItem will still be set to null.
And normally you will get no match , unless it's a value type variable. Maybe this is the problem.
Best Regards
11-12-2009 12:13 PM |
Thanks, guys. When the UI get the data from viewmodel, the way of loading data as below:
Firstly, It will get its datacontext Person which is loaded by ria services. the association Departments(which will be selecteditem for combobox) for this instance is loaded too.
then, in view model, I need to load all Department as data source for combobox dropdown( I loaded in VM constructor).
So it means entity instance Person loaded before the the loading of collection of Departments.
But I can't change Person.Departments after the collection loaded because the right data already there.
If I don't use Combobox to display the dropdown, it is fine. For instance, just display the Departments.Name. But I need the dropdown for data modification.
How to resolve it?
11-12-2009 12:30 PM |
Do not bind the property the SelectedItem of the Combobox directly to your entity. Instead, create a property on your view model to bind to. In the property's get in your ViewModel check to see if the Departments have loaded yet. If they have not return a null instead of the value from the Entity. After the Departments list has loaded do a RaisePropertyChanged for your SelectedItem property which should get everything displayed correctly.