Clear the default selection in SfSegmentedControl

12 Aug 20222 minutes to read

The SfSegmentedControl provides support to clear the default selection on segments by setting the value of SelectedIndex as negative or beyond the collection count.

NOTE

By default, it selects the 0th indexed item.

Namespace:

xmlns:sys="clr-namespace:System.Collections.Generic;assembly=netstandard"
...
   <buttons:SfSegmentedControl
        SelectedIndex="-1"
        BorderColor="#3F3F3F"
        FontColor="Black"
        HorizontalOptions="Center"
        VerticalOptions="Center"
        SegmentHeight="32"
        CornerRadius="20"
        VisibleSegmentsCount="3">
        <buttons:SfSegmentedControl.ItemsSource>
            <sys:List x:TypeArguments="x:String">
                <x:String>Formal</x:String>
                <x:String>Casual</x:String>
                <x:String>Trendy</x:String>
            </sys:List>
        </buttons:SfSegmentedControl.ItemsSource>
    </buttons:SfSegmentedControl>
SfSegmentedControl segmentedControl = new SfSegmentedControl()
    {
        SelectedIndex = -1,
        BorderColor = Color.FromHex("#3F3F3F"),
        FontColor = Color.Black,
        HorizontalOptions = LayoutOptions.Center,
        VerticalOptions = LayoutOptions.Center,
        SegmentHeight = 32,
        CornerRadius = 20,
        VisibleSegmentsCount = 3,
    };

    segmentedControl.ItemsSource = new List<String>()
    {
        "Formal","Casual","Trendy"
    };

Indicates the nullable selection of SfSegmentedControl in Xamarin.Forms