This is a migrated thread and some comments may be shown as answers.

SelectedItem is being set to null when ItemsSource changes

9 Answers 2120 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
jason
Top achievements
Rank 1
jason asked on 14 Sep 2011, 10:10 PM

This issue that is occurring is that whenever the RadComboBox’s ItemsSource property is changed the SelectedItem property is being set to null.

Requirements to recreate issue:

  • Original ItemsSource is a collection of objects that ARE NOT value types
  • SelectedValue is bound to some property
  • SelectedValuePath is set
  • SelectedItem is some item in the original ItemsSource
  • New ItemsSource is a collection of objects of the same type as the original ItemsSource that does not contain a reference to the same object in memory as the current SelectedItem

What is happening is that when the new ItemsSource is set the RadComboBox is finding that none of the new items reference the same object in memory as the current SelectedItem and so setting the SelectedItem to null.

However, I have defined the SelectedValuePath so I clearly want the RadComboBox’s selection mechanism to be driven by the SelectedValue and not the SelectedItem.  What I feel should be happening is that when the ItemsSource changes and the SelectedValuePath is not null the RadComboBox would then compare the value of the property at the SelectedValuePath of the SelectedItem to the value of the property at the SelectedValuePath for each item in the new ItemsSource.  And only then, if it cannot find a match set the SelectedItem to null.

This issue was difficult to track down and has been particularly vexing for us because we are using the Csla framework in our project.  When using this framework, whenever we perform an operation on a business object we are returned a COPY of that object not the original object.  If that object happens to be a collection that a RadComboBox’s ItemsSource is bound to then the SelectedItem is getting wiped out.

9 Answers, 1 is accepted

Sort by
0
Stefan Buys
Top achievements
Rank 2
answered on 15 Sep 2011, 11:32 AM
Hi Jason,

We have exactly the same problem as you describe below. 

After several trial and errors, we found that the underlying Type of your SelectedValue property has to be the same type (or equivalent Nullable<T> type also seems to work) as the value property of the SelectedItem in the ItemsSource.

Give it a try.

Stefan Buys
0
jason
Top achievements
Rank 1
answered on 15 Sep 2011, 03:02 PM
I'm not sure we're experiencing the exact same problem or I'm not fully understanding your description.  

The SelectedValue types in the new collection are of the same type as the old collection.  In fact I can replace the original ItemsSource with a clone of the original ItemsSource where everything is exactly the same except that the objects reside at a different location in memory and the SelectedItem gets wiped out.  I would also point out the the objects at SelectedValuePath ARE value types so they are not being compared by reference.

I have mocked up a sample project demonstrating this but I don't see any way to attach it.  I only have the option to attach images.
0
Stefan Buys
Top achievements
Rank 2
answered on 15 Sep 2011, 03:23 PM
Hi Jason,

Take the below simplified example:

<telerik:RadComboBox x:Name="SearchTypes" ItemsSource="{Binding Path=SearchTypes}" DisplayMemberPath="Description" SelectedValuePath="Id"
         SelectedValue="{Binding Path=QueryCriteria.SearchTypeId, Mode=TwoWay"
         SelectedItem="{Binding Path=SelectedSearchType, Mode=TwoWay">
</telerik:RadComboBox>


In our case, the "SelectedSearchType.Id" and "QueryCritiera.SearchTypeId" types must be the same to avoid setting the SelectedSearchType property to null as you've described. If its the same already, you have a different problem indeed!

NOTE: In terms of changing the ItemsSource, I haven't seen any difference in behaviour between assigning a different object reference collection or a copied collection.

Stefan
0
Brian Sayatovic
Top achievements
Rank 1
answered on 20 Dec 2011, 04:08 PM
I'm having the same issue.  If the ObservableCollection my RadComboBox ItemsSource is bound to changes (e.g. we "reload" by clearing it and asynchronously adding new items back into it), the property bound to SelectedItem or SelectedValue is null'ed out when the collection is emptied.
0
jason
Top achievements
Rank 1
answered on 20 Dec 2011, 04:32 PM
They responded to my company with an email recommending that we override the equals method for any objects that will be used this way.  I'm not completely happy with this solution but it is ultimately what we implemented.  My main issue with overriding the equals method is that, while we are comparing the objects by what would be its primary key in the database, not every object has its primary key set.  This would be the case for new objects created by CSLA that have not been persisted to the database yet.  So any objects with null keys would be equal.  We have yet to run into this scenario in our application but it is a possibility we have to be on guard for when using RadComboBoxes.  

No mention of when this will be fixed or if they even believe this a bug that needs fixing.  
0
Harshal
Top achievements
Rank 1
answered on 13 Aug 2013, 11:18 AM
Hi,

Has above issue been fixed in silverlight RadCombobox.

We are using virtualized Radcombobox in our project. since virtualization does not support filtering so we have created custom filtering mechenism by extending RadCombobox and defined new VirtuzalisedRadCombobox.

We placed this VirtuzalisedRadCombobox in CellEditTemplate. But when we select new value from drop down, then selected value gets null and also itemsource gets null.

Can anybody please let me know whats the reason of the issue.



0
Vladi
Telerik team
answered on 16 Aug 2013, 08:52 AM
Hello,

We are not sure what the issue exactly is. The previously discussed issues is related to when the ItemsSource of the control is changed which causes the SelectedItem to be reset. That issue is resolved by overriding the Equals of the bound objects to the RadComboBox control.

I created and attached a sample project for you that shows the mentioned approach. In the project if you click the change button the SelectedItem (Item 1) will not be reset. If this is not the case could you open a support ticket and send us a sample project that recreated the issue so that we can observe it on our side and see what could be the cause of it?

Regards,
Vladi
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Justin
Top achievements
Rank 1
answered on 13 Sep 2013, 04:54 PM
It looks like the Clear Selection Button event is being fired when the Item Source is set to null.  It shows up in the routed event information in the SelectionChangedEventArgs... sometimes.  Hopefully this will help you solve this issue.  It is causing problems throughout our application and we are considering rolling back our Telerik version.
0
Vladi
Telerik team
answered on 16 Sep 2013, 11:13 AM
Hello,

As we see you have posted a support thread about the same issue could we continue the decision there.

I will post our response from that other thread in order for it to be available for the community:
We are not aware of any issues related to the "Clear Selection" button that could cause the selection to be cleared without the button being clicked.

Regards,
Vladi
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
ComboBox
Asked by
jason
Top achievements
Rank 1
Answers by
Stefan Buys
Top achievements
Rank 2
jason
Top achievements
Rank 1
Brian Sayatovic
Top achievements
Rank 1
Harshal
Top achievements
Rank 1
Vladi
Telerik team
Justin
Top achievements
Rank 1
Share this question
or