Home > front end >  Radio button in xamarin form
Radio button in xamarin form

Time:11-24

In xamarin form cross platform My problem is, can I put the radio button to a container then I will set an ID to container so that I will access the radiobtn content via the container's ID. I make an code for this, Idk if its correct sir. `

 <StackLayout RadioButtonGroup.GroupName="ID_type">
                                <RadioButton Content="National ID"/>
                                <RadioButton Content="Voters ID"/>
                                <RadioButton Content="Drivers License"/>
                                <RadioButton Content="PRC ID"/>
                                <RadioButton Content="Postal ID"/>

                                <RadioButton Content="Others:"/>
                            </StackLayout>

`

Because it is hassle to put ID in each content of radiobutton. any link or article that will be commented, will be appreciated.

CodePudding user response:

Since the parent is StackLayout, you can traverse its children by referring to property x:Name of StackLayout.

Please refer to the following code:

   <StackLayout RadioButtonGroup.GroupName="fruits" x:Name="mStackLayout"> 
        <!-- All of the RadioButtons in this StackLayout will automatically be given the
             GroupName 'fruits' -->
        <RadioButton Content="Apple" />
        <RadioButton Content="Banana" />
        <RadioButton Content="Pineapple" />
        <RadioButton Content="Other" />
    </StackLayout>

    <Button  Text="test" Clicked="Button_Clicked"/>

Function Button_Clicked:

private void Button_Clicked(object sender, System.EventArgs e) 
    {

        var children = mStackLayout.Children;

        foreach (var child in children) {

            if (child is RadioButton) {

                RadioButton button = (RadioButton) child;

                System.Diagnostics.Debug.WriteLine(" one child 's content is : "   button.Content);
            
            }
        }
    }
  • Related