SelectedItem is being set to null when ItemsSource changes

10 posts, 0 answers
  1. jason
    jason avatar
    4 posts
    Member since:
    Jun 2011

    Posted 14 Sep 2011 Link to this post

    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.

  2. Stefan Buys
    Stefan Buys avatar
    22 posts
    Member since:
    Dec 2009

    Posted 15 Sep 2011 Link to this post

    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
  3. DevCraft banner
  4. jason
    jason avatar
    4 posts
    Member since:
    Jun 2011

    Posted 15 Sep 2011 Link to this post

    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.
  5. Stefan Buys
    Stefan Buys avatar
    22 posts
    Member since:
    Dec 2009

    Posted 15 Sep 2011 Link to this post

    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
  6. Brian Sayatovic
    Brian Sayatovic avatar
    91 posts
    Member since:
    Nov 2009

    Posted 20 Dec 2011 Link to this post

    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.
  7. jason
    jason avatar
    4 posts
    Member since:
    Jun 2011

    Posted 20 Dec 2011 Link to this post

    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.  
  8. Harshal
    Harshal avatar
    10 posts
    Member since:
    Jul 2013

    Posted 13 Aug 2013 Link to this post

    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.



  9. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 16 Aug 2013 Link to this post

    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 >>
  10. Justin
    Justin avatar
    4 posts
    Member since:
    Dec 2011

    Posted 13 Sep 2013 Link to this post

    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.
  11. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 16 Sep 2013 Link to this post

    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 >>
Back to Top
DevCraft banner