title: In the C# back end, how can I select an element in a Segmented Control?
body: <p>I'm using a custom control to create joined buttons that display like this:</p>
  
  <pre><code>********************************************
  *            *            *                *
  *    One     *    Two     *     Three      *
  *            *            *                *
  ********************************************
  </code></pre>
  
  <p>When I click the buttons then an event is fired:</p>
  
  <pre><code>void OnValueChanged(object sender, EventArgs e)
  {
       switch (segControl.SelectedValue)
       {
  </code></pre>
  
  <p>This is working well but what I would like to do is to be able to select one of those buttons from within my C# code. </p>
  
  <p>When I look at the source code for the control I cannot see how to do this. </p>
  
  <p>Would appreciate if anyone has any suggestions on what I should do to for example select the 2nd button in the same way as if I had clicked on it. </p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>&lt;local:SegmentedControl ValueChanged="OnValueChanged" SelectedSegment="{Binding CustomPointsSwitch}" x:Name="segControl" HorizontalOptions="End"&gt;
     &lt;local:SegmentedControl.Children&gt;
         &lt;local:SegmentedControlOption Text="Two" /&gt;
         &lt;local:SegmentedControlOption Text="Four" /&gt;
     &lt;/local:SegmentedControl.Children&gt;
  &lt;/local:SegmentedControl&gt;
  </code></pre>
  
  <p><strong>Shared Code</strong></p>
  
  <pre><code>public class SegmentedControl : View, IViewContainer&lt;SegmentedControlOption&gt;
  {
      public IList&lt;SegmentedControlOption&gt; Children { get; set; }
  
      public SegmentedControl()
      {
          Children = new List&lt;SegmentedControlOption&gt;();
      }
  
      public event ValueChangedEventHandler ValueChanged;
  
      public delegate void ValueChangedEventHandler(object sender, EventArgs e);
  
      private string selectedValue;
  
      public string SelectedValue
      {
          get { return selectedValue; }
          set
          {
              selectedValue = value;
              if (ValueChanged != null)
                  ValueChanged(this, EventArgs.Empty);
          }
      }
      public static readonly BindableProperty SelectedSegmentProperty = BindableProperty.Create("SelectedSegment", typeof(int), typeof(SegmentedControl), 0);
      public int SelectedSegment
      {
          get
          {
              return (int)GetValue(SelectedSegmentProperty);
          }
          set
          {
              SetValue(SelectedSegmentProperty, value);
          }
      }
  }
  
  public class SegmentedControlOption : View
  {
      public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(SegmentedControlOption), string.Empty);
  
      public string Text
      {
          get { return (string)GetValue(TextProperty); }
          set { SetValue(TextProperty, value); }
      }
  }
  </code></pre>
  
  <p><strong>iOS Renderer</strong></p>
  
  <pre><code>public class SegmentedControlRenderer : ViewRenderer&lt;SegmentedControl, UISegmentedControl&gt;
  {
      public SegmentedControlRenderer()
      {
      }
  
      protected override void OnElementChanged(ElementChangedEventArgs&lt;SegmentedControl&gt; e)
      {
          base.OnElementChanged(e);
  
          var segmentedControl = new UISegmentedControl();
          for (var i = 0; i &lt; e.NewElement.Children.Count; i++)
          {
              segmentedControl.InsertSegment(e.NewElement.Children[i].Text, i, false);
          }
  
          segmentedControl.ValueChanged += (sender, eventArgs) =&gt; {
              e.NewElement.SelectedSegment = (int)segmentedControl.SelectedSegment;
              e.NewElement.SelectedValue = segmentedControl.TitleAt(segmentedControl.SelectedSegment);
          };
  
          SetNativeControl(segmentedControl);
      }
  }
  </code></pre>
  
  <p><strong>Android Renderer</strong></p>
  
  <pre><code>public class SegmentedControlRenderer : ViewRenderer&lt;SegmentedControl, RadioGroup&gt;
  {
  
      protected override void OnElementChanged(ElementChangedEventArgs&lt;SegmentedControl&gt; e)
      {
          base.OnElementChanged(e);
  
          var layoutInflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
  
          var g = new RadioGroup(Context);
          g.Orientation = Orientation.Horizontal;
          g.CheckedChange += (sender, eventArgs) =&gt; {
              var rg = (RadioGroup)sender;
              if (rg.CheckedRadioButtonId != -1)
              {
                  var id = rg.CheckedRadioButtonId;
                  var radioButton = rg.FindViewById(id);
                  var radioId = rg.IndexOfChild(radioButton);
                  var btn = (RadioButton)rg.GetChildAt(radioId);
                  var selection = (String)btn.Text;
                  e.NewElement.SelectedValue = selection;
              }
          };
  
          for (var i = 0; i &lt; e.NewElement.Children.Count; i++)
          {
              var o = e.NewElement.Children[i];
              var v = (SegmentedControlButton)layoutInflater.Inflate(Resource.Layout.SegmentedControl, null);
              v.Text = o.Text;
              if (i == 0)
                  v.SetBackgroundResource(Resource.Drawable.segmented_control_first_background);
              else if (i == e.NewElement.Children.Count - 1)
                  v.SetBackgroundResource(Resource.Drawable.segmented_control_last_background);
              g.AddView(v);
          }
  
          SetNativeControl(g);
      }
  }
  
  public class SegmentedControlButton : RadioButton
  {
      private int lineHeightSelected;
      private int lineHeightUnselected;
      private Paint linePaint;
  
      public SegmentedControlButton(Context context, IAttributeSet attributes) : this(context, attributes, Resource.Attribute.segmentedControlOptionStyle)
      {
      }
  
      public SegmentedControlButton(Context context, IAttributeSet attributes, int defStyle) : base(context, attributes, defStyle)
      {
          Initialize(attributes, defStyle);
      }
  
      private void Initialize(IAttributeSet attributes, int defStyle)
      {
          var a = this.Context.ObtainStyledAttributes(attributes, Resource.Styleable.SegmentedControlOption, defStyle, Resource.Style.SegmentedControlOption);
  
          var lineColor = a.GetColor(Resource.Styleable.SegmentedControlOption_lineColor, 0);
          linePaint = new Paint();
          linePaint.Color = lineColor;
  
          lineHeightUnselected = a.GetDimensionPixelSize(Resource.Styleable.SegmentedControlOption_lineHeightUnselected, 0);
          lineHeightSelected = a.GetDimensionPixelSize(Resource.Styleable.SegmentedControlOption_lineHeightSelected, 0);
  
          a.Recycle();
      }
  
      protected override void OnDraw(Canvas canvas)
      {
          base.OnDraw(canvas);
  
          if (linePaint.Color != 0 &amp;&amp; (lineHeightSelected &gt; 0 || lineHeightUnselected &gt; 0))
          {
              var lineHeight = Checked ? lineHeightSelected : lineHeightUnselected;
  
              if (lineHeight &gt; 0)
              {
                  var rect = new Rect(0, Height - lineHeight, Width, Height);
                  canvas.DrawRect(rect, linePaint);
              }
          }
      }
  }
  </code></pre>
  
  <p>I would appreciate any suggestions on how I can make the change.  Also does anyone have an insights as to if Xamarin are looking to make this into a forms component?</p>