locked
Weird problem with ComboBox

    Question

  • Hi,

    Look at the following image.  I get the error "Value does not fall within the expected range" I've been trying to understand since 2 days and now I need help.  I've inspected every single variable and collections in there and everything is fine.  First of all, the error is reported on the wrong line because the faulty line is when I try to set the SelectedItem of the ComboBox.  Prof is that if I remove the line, everything is fine. I honestly don't know what is causing it.  I even tried to put the assignment of the SelectedItem in an await Dispatcher.RunAsync() but with no luck !  So what doesn't fall within the expected range ? What is the expected range and where do I have to look to solve that mystery ?

     

     

    http://1drv.ms/M5SQHb




    • Edited by GearWorld Monday, February 17, 2014 10:54 AM
    Monday, February 17, 2014 10:45 AM

Answers

  • Sorry about that. It was just because I was using List<> rather than ObservableCollection<>

    • Marked as answer by GearWorld Monday, February 17, 2014 8:08 PM
    Monday, February 17, 2014 8:07 PM

All replies

  • Looks like there's nothing wrong with the xaml :

    <ComboBox x:Name="cboProfiles" Margin="5,10,5,0" SelectionChanged="cboProfiles_SelectionChanged" SelectedValuePath="PlayerIdentity" DisplayMemberPath="PlayerName"/>

    Also, everything is fine if I already have one item in the ComboBox. The problem occurs when it's the first item I add to it. It is also bound to the List<Profile> which Profile implements INotifyPropertyChanged all right

    Monday, February 17, 2014 10:53 AM
  • This example should show how to access the value in the combo box:

    string s = ComboBoxMainComponent.SelectedValue.ToString();

    You should also set the placeholdertext property to the value you want pre selected in the combo box.


    Also if it is the only item you may want to change the >1 to >0.
    • Edited by RogerPicard Monday, February 17, 2014 3:45 PM
    Monday, February 17, 2014 3:41 PM
  • You cannot set the selection to something not available in the ComboBox. The SelectedItem should already be included in the bound DataContext.

    The PlaceholderText Roger references isn't directly related to the selection. It isn't a default value. The placeholder is instructional text to display when nothing is shown.

    --Rob

    Monday, February 17, 2014 4:10 PM
    Owner
  • Rob, I added the element in the collection bound to the ComboBox as normal and when I inspect the ComboBox I see the element there all right.

    Monday, February 17, 2014 4:19 PM
  • Can you share a minimal repro on your SkyDrive so we can see the problem in action?

    --Rob

    Monday, February 17, 2014 4:38 PM
    Owner
  • If you are a Microsoft professional I could give you the entire project Would it be best ?


    • Edited by GearWorld Monday, February 17, 2014 5:00 PM
    Monday, February 17, 2014 5:00 PM
  • Strangely, I tried using the same technic in a Silverlight 5 app repro project and I receive this message :

    GeneratorPosition '-1,1' passed to Remove does not have Offset equal to 0.

    Monday, February 17, 2014 5:14 PM
  • There's definitely something wrong I'm doing. I'll just take a moment to do it in a different way to see if I can scratch something out of it
    Monday, February 17, 2014 5:15 PM
  • I think all this is because I program too much and I lost a bit of basic knowledge.  I should smash my head against a wall here. 

    I'm not sure yet but at least in Silverlight 5 repro I did it solved the problem.  Will have to try at home in the WSA app.  I was having a List<profile> rather than an ObservableCollection<profile>

    • Marked as answer by GearWorld Monday, February 17, 2014 8:07 PM
    • Unmarked as answer by GearWorld Monday, February 17, 2014 8:08 PM
    Monday, February 17, 2014 5:22 PM
  • Sorry about that. It was just because I was using List<> rather than ObservableCollection<>

    • Marked as answer by GearWorld Monday, February 17, 2014 8:08 PM
    Monday, February 17, 2014 8:07 PM
  • Thanks for the update. I'm glad to hear you found this. It's difficult for us to help without the details on both ends of the binding.

    The forums are for community based support, so anything done at this level needs to be appropriate for the public. If you need individual support (like send a private repro) you can open a case at http://aka.ms/storesupport . In either case, a minimal stripped down repro with just enough code to reproduce the problem is generally much more efficient than to work with than a full project.

    --Rob

    Monday, February 17, 2014 10:26 PM
    Owner
  • Ok Thank you

    Monday, February 17, 2014 11:09 PM