We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Change dynamically the SfSegmentedControl.ItemsSourceProperty

Hello,
I have a SfSegmentedControl with three segments and I'd like to know how to change the SfSegmentedControl.ItemsSourceProperty when a Clicked button event is triggered?
I'm not using XAML, just C#.
Thanks,

3 Replies

HM Hemalatha Marikumar Syncfusion Team December 12, 2019 11:25 AM UTC

Hi Carlos, 
  
Greetings from Syncfusion. 
  
We have checked your requirement and it looks like modifying the entire ItemsSource of SfSegmentedControl by adding new items or removing the existing item or replace the existing ItemsSource with new one. Based on your request, we have prepared a sample with the supported item in ItemsSource with using the ObservableCollection. Please download the same in below link 
  
  
Code Snippet [C#]: 
  
public partial class MainPage : ContentPage 
    { 
        SfSegmentedControl stringSegmentControl; 
  
        SfSegmentedControl segmentControl; 
  
        public MainPage() 
        { 
            InitializeComponent(); 
  
            StackLayout stackLayout = new StackLayout(); 
  
            stringSegmentControl = new SfSegmentedControl() 
            { 
                ItemsSource = new ObservableCollection<string>() { "Segment 1""Segment 2""Segment 3" }, 
                Margin = new Thickness(0, 40, 0, 0) 
            }; 
  
            segmentControl = new SfSegmentedControl() 
            { 
                ItemsSource = new ObservableCollection<SfSegmentItem>() 
                { 
                    new SfSegmentItem(){Text = "Segment 1"}, 
                    new SfSegmentItem(){Text = "Segment 2"}, 
                    new SfSegmentItem(){Text = "Segment 3"}, 
                }, 
                Margin = new Thickness(0, 40, 0, 0) 
            }; 
  
            Button dynamicUpdate = new Button() { Text = "Click to Update" }; 
            dynamicUpdate.Clicked += DynamicUpdate_Clicked; 
  
            stackLayout.Children.Add(stringSegmentControl); 
            stackLayout.Children.Add(segmentControl); 
            stackLayout.Children.Add(dynamicUpdate); 
  
            this.Content = stackLayout; 
            
        } 
  
        private void DynamicUpdate_Clicked(object sender, EventArgs e) 
        { 
            var stringCollection = (stringSegmentControl.ItemsSource as ObservableCollection<string>); 
            stringCollection.Add("Segment " + (stringCollection.Count + 1).ToString()); 
  
            var segmentCollection = (segmentControl.ItemsSource as ObservableCollection<SfSegmentItem>); 
            segmentCollection.Add(new SfSegmentItem() { Text = "Segment " + (segmentCollection.Count + 1).ToString() }); 
  
        } 
    } 
  
Please let us know if you need any further assistance. 
 
Regards, 
Hemalatha M. 



CA Carlos December 12, 2019 12:43 PM UTC

Thanks, that's what I need.


HM Hemalatha Marikumar Syncfusion Team December 13, 2019 04:50 AM UTC

Hi Carlos, 
 
Thanks for your update. 
 
We are glad to hear that given solution meets your need. 
 
Please let us know if you need any further assistance. 
 
Regards, 
Hemalatha M. 


Loader.
Live Chat Icon For mobile
Up arrow icon