C1ComboBox - Setting SelectedItem throwing exception

Posted by: jared.fritsch on 6 February 2018, 9:47 am EST

    • Post Options:
    • Link

    Posted 6 February 2018, 9:47 am EST

    Xamarin.Forms

    C1 version: v2.5.20173.241

    Tested platforms: UWP

    When using a list of strings as the ItemsSource to my C1ComboBox, setting the SelectedItem property throws a System.ArgumentOutOfRangeException. This seems to be related to the fact that I am using a list of strings as my data source. If I wrap those strings in some sort of other class, it works as expected.

    private string str1 = "test2";
    
    public void Setup()
    {
        var itemsSource = new List<string>()
        {
            "test1",
    	str1,
    	"test3",
        };
    
        TheComboBox.ItemsSource = itemsSource;
    }
    
    private void Button_OnClicked(object sender, EventArgs e)
    {
        TheComboBox.SelectedItem = str1;
    }
    
  • Posted 6 February 2018, 7:16 pm EST

    Since there isn’t any dataitem when binding the ComboBox to a string array, the exception is thrown. Please try setting the SelectedValue property or the SelectedIndex property.

  • Posted 7 February 2018, 1:09 am EST

    So that exception is by design then?

    Also, correct me if I’m wrong, but setting the SelectedValue doesn’t actually changed the selection, it just changes the value of the currently selected item. Using the code above as a reference, if my ComboBox shows a SelectedIndex of 2, and I set the SelectedValue to str1, the SelectedIndex will stay at 2, but now the SelectedValue will shows as “test2”. The documentation confirms this as well, saying “Gets or sets the value of the currently selected item using SelectedValuePath.”

    Using SelectedIndex does work just fine. We just now have to write some extra code that we figured setting SelectedItem would do for us.

    var index = ((List<string>)TheComboBox.ItemsSource).IndexOf(str1);
    TheComboBox.SelectedIndex = index;
    
  • Posted 8 February 2018, 5:12 pm EST

    This seems to be an odd behavior. Let me get in touch with the development team regarding this and get back to you. (Tracking Id - 308430)

  • Posted 2 April 2018, 4:56 pm EST

    Hi,

    The bug is fixed in the latest build (2.5.20181.267) and is available on Nuget. Kindly update your application with this build.

    Thanks

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels