none
ComboBox - "Value does not fall within the expected range"

    Question

  • I can consistently reproduce the exception "Value does not fall within the expected range" in a combo box.

    The steps are:

    • populate a combo box, set its selectedindex / selecteditem
    • from the UI click any option in the combobox -  even the currently selected one
    • populate the combo box with new items, set the selectedindex

    The last step raises the exception.

    An example in code - add this to a default Silverlight 2 Visual Studio project.  You can click the 'Rotate' button as many times as you want, select an option in the ComboBox, then click 'Rotate' and the exception occurs.

    XAML:

     <UserControl x:Class="BreakCombo.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="400" Height="300" Loaded="UserControl_Loaded">
        <Grid x:Name="LayoutRoot" Background="White">
            <StackPanel>
                <Button Content="Rotate" Click="Button_Click"></Button>
                <ComboBox x:Name="cmbNumbers">
                    
                </ComboBox>
                
            </StackPanel>
        </Grid>
    </UserControl>

    Code-behind:

        public partial class Page : UserControl
        {
            List<int> even = new List<int>(new int[] { 2, 4, 6, 8, 10 });
            List<int> odd  = new List<int>(new int[] { 1, 3, 5, 7, 9 });

            bool evenList = true;

            public Page()
            {
                InitializeComponent();
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                PopulateNumbers();
            }

            private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
                PopulateNumbers();
               
            }

            private void PopulateNumbers()
            {
                if (evenList)
                {
                    cmbNumbers.ItemsSource = odd;
                }
                else
                {
                    cmbNumbers.ItemsSource = even;
                }
                cmbNumbers.SelectedIndex = 2;
                evenList = !evenList;
            }
        }

    Sunday, January 18, 2009 6:32 AM

Answers

  • Change the method like below:

            private void PopulateNumbers()
            {
                if (evenList)
                {
                    cmbNumbers.ItemsSource = odd;
                }
                else
                {
                    cmbNumbers.ItemsSource = even;
                }
                cmbNumbers.UpdateLayout();
                cmbNumbers.SelectedIndex = 2;
                evenList = !evenList;
            }

    Hope this will help you.

    Please Mark as Answer if this helps you.

    Sunday, January 18, 2009 9:15 AM

All replies

  • Change the method like below:

            private void PopulateNumbers()
            {
                if (evenList)
                {
                    cmbNumbers.ItemsSource = odd;
                }
                else
                {
                    cmbNumbers.ItemsSource = even;
                }
                cmbNumbers.UpdateLayout();
                cmbNumbers.SelectedIndex = 2;
                evenList = !evenList;
            }

    Hope this will help you.

    Please Mark as Answer if this helps you.

    Sunday, January 18, 2009 9:15 AM
  • This does fix this issue I'm reproducing here.  Is it actually a bug - or should I be using UpdateLayout() everytime a control changes contents?

    The annoying thing is that I'm getting this error in a form that uses databinding - so I don't have a place to call UpdateLayout().

    Sunday, January 18, 2009 3:29 PM
  • Here is an example of the same issue happening in databinding code: http://blog.time2call.net/BreakComboDatabinding/TestPage.html

    Toggle between Even/Odd everything is fine.  The click an item in the ComboBox, and try toggle Even/Odd and it will throw the "Value does not fall within the expected range" exception.

    Source code is here: http://blog.time2call.net/BreakComboDatabinding.zip

    In the Page.xaml.cs I've implemented a PageViewModel_PropertyChanged - and tried to call UpdateLayout from there.  No luck.

    Sunday, January 18, 2009 5:46 PM
  • You actually don't even need to select an item in the combobox, just click to open it.

    I'm having the same issue, databinding to ItemsSourceProperty and SelecteditemProperty and its blowing up everywhere. Pretty much makes cascading comboboxes impossible with databinding and I haven't had much luck just setting the ItemsSource manually either.

    Anyone know of a third party combobox worth using until this is resolved? 

    Wednesday, February 18, 2009 5:03 PM
  •  hi jpy,

     I've been experimenting with cascading comboboxes myself.  I think i'm onto a solution.  I'll post a follow up to this thread if/when I get a fix..

    russau

    Wednesday, February 18, 2009 5:53 PM
  • There is a GDR, don't get your hopes up though, didn't fix my issue.

     http://timheuer.com/blog/archive/2009/02/19/silverlight-2-gets-minor-update-gdr1.aspx

     

    Friday, February 20, 2009 10:56 AM
  • Yes, I quicky browse the features of the GDR hoping for some fixes to issues I'm looking at.

    I did mange to get a 'working' solution for cascading ComboBoxes - see the solution here:

    http://tinisles.blogspot.com/2009/02/silverlight-2-mvvm-databinding_18.html

     

    Saturday, February 21, 2009 2:15 AM
  • Hi russau,

    I am facing the same problem and am very interested in seeing your fix. But I am unable to reach the url. Is the url correct? Please let me know. Thank you.

    -etrast-

    Tuesday, February 24, 2009 5:24 AM
  •  oops - a space snuck in when I hyperlinked the article.  fixed now: http://tinisles.blogspot.com/2009/02/silverlight-2-mvvm-databinding_18.html

    Tuesday, February 24, 2009 5:36 AM
  •  

    There Seems to be an issue in Silverlight Combobox 2.0.

    Exception:

    Value does not fall within the expected range.

       at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData)

       at MS.Internal.XcpImports.MethodPack(IntPtr objectPtr, String methodName, Object[] rawData)

       at MS.Internal.XcpImports.UIElement_TransformToVisual(UIElement element, UIElement visual)

       at System.Windows.UIElement.TransformToVisual(UIElement visual)

       at System.Windows.Controls.Primitives.Selector.IsOnCurrentPage(Int32 index, Rect& itemsHostRect, Rect& listBoxItemRect)

       at System.Windows.Controls.Primitives.Selector.ScrollIntoView(Int32 index)

       at System.Windows.Controls.Primitives.Selector.SetFocusedItem(Int32 index, Boolean scrollIntoView)

       at System.Windows.Controls.ComboBox.PrepareContainerForItemOverride(DependencyObject element, Object item)

       at iQor.Pars.SilverLight.Controls.IqorComboBox.PrepareContainerForItemOverride(DependencyObject element, Object item)

     

    Combobox in Silverlight seems to have some issue while setting the SelectedItem or index. I tried with updatedLayout, but still it throw exeception. So I have derived the control and override the PrepareContainerForItemOverride method and call the base method with try catch. This solved the issue and Selected item seems to be working fine. This may not be bestway. But this is working fine for me.

     

    1    public class MyCombo : ComboBox
    2    {
    3        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
    4        {
    5            try
    6            {
    7                base.PrepareContainerForItemOverride(element, item);
    8            }
    9            catch
    10           { }
    11       }
    12   }
    13   
    

     Let me know if this work fine for you all.

    Thursday, June 18, 2009 8:38 AM
  •  This works great! Thanks!

    Tuesday, August 18, 2009 11:35 AM
  • I guess this is not the solution, the problem is when you are using a List and when you're changing its items. The ItemsSource of the combobox does not know the list items changed and it crashes after you specify the a new selected item. Try an Observable collection and you'll the see the difference

    Wednesday, February 02, 2011 1:23 PM
  • I was getting "Value does not fall within the expected range." on a control I am writing. The error messege is useless and there is no stack trace and the catch block does not work in my application startup.  More on error handling in a different thread.

    Anyway, it turns out I was not using a dependency property when I should have. 

     

    My control just has a combo box like this:

    <ComboBox 
            DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"
            SelectedValue="{Binding SelectedSite}"
            ItemsSource="{Binding Sites}"  
            DisplayMemberPath="SiteName"
            SelectedValuePath="SiteID"
            x:Name="Combo">
        </ComboBox>

     

     

    SelectedSite is exposed as a public property.  I had to make it a dependency property like this:

    public int SelectedSite
            {
                get { return (int)GetValue(SelectedSiteProperty); }
                set { SetValue(SelectedSiteProperty, value); }
            }
    
            public static readonly DependencyProperty SelectedSiteProperty =
                DependencyProperty.Register("SelectedSite", typeof(int), typeof(SitesCombo), new PropertyMetadata(0));

     

    Now, in the page that hosts my control I can bind to SelectedSite:

    <localControls:SitesCombo x:Name="SitesCombo" Width="150" Margin="0,0,10,0" SelectedSite="{Binding SelectedSiteID, Mode=TwoWay}" ></localControls:SitesCombo>

     

    Send money if this helps you.  Or if it dosent.

    Monday, March 12, 2012 12:02 AM