Skip to main content

Microsoft Silverlight

Unanswered Question combobox data binding problemRSS Feed

(0)

kent.zhou
kent.zhou

Member

Member

109 points

397 Posts

combobox data binding problem

 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
Ardman

Contributor

Contributor

3336 points

922 Posts

Re: combobox data binding problem

 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)

kent.zhou
kent.zhou

Member

Member

109 points

397 Posts

Re: combobox data binding problem

 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
ColinBlair

Contributor

Contributor

6579 points

1,291 Posts

Re: combobox data binding problem

If the ComboBox ever finds that the bound SelectedItem does not exist in the bound ItemsSource then it breaks the binding on SelectedItem.

-Colin Blair

http://www.RiaServicesBlog.net : The Elephant Guide to RIA Services
SLColinBlair on Twitter

Min-Hong Tang - MSFT
Min-Hong...

Contributor

Contributor

3375 points

377 Posts

Re: combobox data binding problem

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

Min-Hong Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

kent.zhou
kent.zhou

Member

Member

109 points

397 Posts

Re: combobox data binding problem

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?

 

 

ColinBlair
ColinBlair

Contributor

Contributor

6579 points

1,291 Posts

Re: combobox data binding problem

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.

-Colin Blair

http://www.RiaServicesBlog.net : The Elephant Guide to RIA Services
SLColinBlair on Twitter
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities