by default SegmentedControls SelectedIndex is -1. In my code SegmentedControls ItemsSource is databound to the view model, because the buttons are generated dynamically. If you initialize SelectedIndex with 0 (or greater) before the ItemsSource databound property is allocated in the view model (= null), SegementedControl throws a NullReferenceException. On valid ItemsSource and if you set SelectedIndex to any number greater than the number of items in ItemsSource a NullReferenceException is thrown also.
Xamarin.Forms.Picker uses SelectedIndex too. It ignores the SelectedIndex value if ItemsSource is null or in case SelectedIndex exceeds the number of items in ItemSource, the last item is selected.
5 Answers, 1 is accepted
This is expected behavior as you cannot set an index that is out of the available range (the exception should be an OutOfRangeException instead).
To clarify about the -1 value, is the typical default SelectedIndex when nothing is selected. Such is the case when there are no items yet. If you have a view model property two-way bound to the control, you should always initialize that property to -1 instead of 0. The SegementedControl will change it to 0 after the ItemsSource is set.
On the other side of things, you should always check the item count before setting an index:
Although the Xamarin.Forms Picker lets you set an out of bound value, and it shifts to the next available value, this is not the typical behavior of items controls. If there isn't an index available, it should not select an invalid location instead (first or last item).
In any case, I have submitted a feature request to development team to consider adding a Picker-like behavior: SegmentedControl SelectedIndex Bounds. Please take a moment to up-vote/follow the item and leave a comment if you have any other info you'd like to add.
I hope this info was helpful.
Lance | Tech Support Engineer, Sr.
thank you for your message. You are right, SelectedIndex is databound to my view model too and I initialized it with 0, but the ItemsSource was loaded async via Task in my view model. That's why ItemsSource was still null when Segmented Control read the databound SelectedIndex. I fixed my view model immediately. I think the NullReferenceExceptions is thrown from the underlying framework, because Segemented Control accesses it's ItemsSource without null-check, that's why I was writing here.
What I know from my experience with WPF, controls do not throw exceptions on problematic values coming from databinding. I think they see databinding more or less as a lightweight or loose connection, which does not throw hard exceptions, but ignores.
The example with setting SelectedIndex to an out of bounds value was just a test I ran to see what Segmented Control does and what Framework controls do, it was not from my programming.
That's all, thank you and have a nice day.
I am sorry for the negative experience you had with RadSegmentedControl.
I completely agree the behavior could be improved and the exception should be handled internally. We have this logged in the referenced feedback item and will definitely take care of it when working on the issue.
Yes it is just really hard to debug these types of exceptions in Xamarin XAML. If it threw a better error message instead of just NULL OBJECT ERROR. Maybe like "index references an object that does not exist in your data source". Just something a little more descriptive