Forum Xamarin.Forms

Radiobuttons in xamarin forms

User_101User_101 Member ✭✭✭
edited December 2019 in Xamarin.Forms

I am using InputKit plugin to use radio buttons.

 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">
        <input:RadioButtonGroupView  x:Name="gender" SelectedItemChanged="genderSelected">
            <input:RadioButton Text="Male"  />
            <input:RadioButton Text="Female" />
        </input:RadioButtonGroupView>

    </StackLayout>

I want to read the values from radio buttons.I am trying to use the x:Name attribute but I get errors for the same behind the .cs file.

namespace Testing
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Greet : ContentPage
   {
    public Greet()
    {
        InitializeComponent();

    }

    public void genderSelected(object sender, EventArgs e)
    {
        var value = gender.Items <------here gender is not defined which I want to access from x:Name attribute
    }
Tagged:

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Check source code of RadioButtonGroupView , there is no available property called Items .

    Get the values by retrieving the RadioButtonGroupView.Children.

     private void gender_SelectedItemChanged(object sender, EventArgs e)
        {
            var list = new List<string>();  // it is what you want 
    
            foreach(var item in gender.Children)
            {
                var button = item as RadioButton;
                list.Add(button.Text);
            }
        }
    
Sign In or Register to comment.