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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Change dynamically the SfSegmentedControl.ItemsSourceProperty

Thread ID:

Created:

Updated:

Platform:

Replies:

149853 Dec 11,2019 04:18 PM UTC Dec 13,2019 04:50 AM UTC Xamarin.Forms 3
loading
Tags: SfSegmentedControl
Carlos
Asked On December 11, 2019 04:18 PM UTC

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,

Hemalatha Marikumar [Syncfusion]
Replied On 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. 


Carlos
Replied On December 12, 2019 12:43 PM UTC

Thanks, that's what I need.

Hemalatha Marikumar [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon