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

Getting "SelectedItem is not contained within the ItemsSource"

2 Answers 137 Views
ListPicker
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
catqbat
Top achievements
Rank 1
catqbat asked on 01 Jul 2014, 05:32 PM
Hello,

I'm experiencing strange issue with RadListPicker.

This is how the xaml part looks like:

<telerikInput:RadListPicker 
ItemsSource="{Binding AvailableUsers}" SelectedItem="{Binding SelectedUser, Mode=TwoWay}"
DisplayMemberPath="Login"  
NoSelectionContent="Select user..."
 />

The control is bound to ViewModel, which contains two properties: observable collection "AvailableUsers", and "SelectedUser" of proper type. 
On every page load collection is being populated with data which comes from web service.

At the initial page load everything works fine, but when I navigate away and then come back, I'm getting "SelectedItem is not contained within the ItemsSource" error.

This is how the code looks like:

AvailableUsers = null;
SelectedUser = null;

AvailableUsers = e.Result; //<-- this throws Exception

e.Result is data from web service containing collection of users. The last line throws Exception and when the debugger stops in this point I can see that SelectedUser is not null (take a look at my attached screenshot).
So when this happens app continues to run, but list picker displays "Select user..." string - as if there was no selection. When I select an item, viewmodel is being notifed but control still displays "Select user..." you can't see the change.


The issue is quite tricky, I tried to make simplified test solution with similar scenario, but was unable to get the error.


Any ideas?


2 Answers, 1 is accepted

Sort by
0
Rosy Topchiyska
Telerik team
answered on 04 Jul 2014, 12:30 PM
Hello Agata,

Thank you for contacting us.

From the provided information it is not completely clear what is your scenario, but we succeeded to reproduce such error when using a static view model. Could you please try to replace this code:
AvailableUsers = null;
SelectedUser = null;
AvailableUsers = e.Result;
With:
this.AvailableUsers.Clear();
foreach (var user in e.Result)
{
    this.AvailableUsers.Add(user);
}
And let us know if this works fine. If not, we will need some more information about your scenario: could you please send us a sample project that replicates the navigation logic and the view model bindings from your app.

I look forward to your reply.

Regards,
Rosy Topchiyska
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
catqbat
Top achievements
Rank 1
answered on 05 Jul 2014, 04:29 PM
Yes, this approach does solve the issue :)

Thank you!
Tags
ListPicker
Asked by
catqbat
Top achievements
Rank 1
Answers by
Rosy Topchiyska
Telerik team
catqbat
Top achievements
Rank 1
Share this question
or