Home > OS >  Is there a way to set the Xamarin radiobutton CheckedChanged method in c#?
Is there a way to set the Xamarin radiobutton CheckedChanged method in c#?

Time:06-24

Working on a basic proof of concept trying to build an entire view using information from a database. Based on what I have so far:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="FieldDataTemplateSelectorSample.DynamicView">
    <ContentPage.Content>
        <StackLayout>
            <StackLayout Spacing="2" x:Name="EntryFieldsStack">
                <!-- empty in xaml because we add details in code behind -->
            </StackLayout>
            <Label Text="nothing selected" x:Name="SelectedLabelName" />
        </StackLayout>

    </ContentPage.Content>
</ContentPage>

and the code behind so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using FieldDataTemplateSelectorSample.Validations;

namespace FieldDataTemplateSelectorSample
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DynamicView : ContentPage
    {
        public DynamicView()
        {
            InitializeComponent();
            LoadDynamicFields();
        }

        public void OnRadioChanged(object sender, CheckedChangedEventArgs e)
        {
            SelectedLabelName.Text = String.Format("Selected value is {0}", e.Value);
        }

        public void LoadDynamicFields()
        { 
            EntryFieldsStack.Children.Clear();
            EntryFieldsStack.Children.Add(new Label { Text = "Dynamic Radio"});
            for (int i = 0; i < 10; i  )
            {               
                EntryFieldsStack.Children.Add(new RadioButton 
                { 
                    Value = i.ToString(), 
                    Content = string.Format("Radio text {0}", i.ToString()),
                    GroupName = "RadioGroup1"
                });
            }
        }
    }
}

So for now it is hard-coded to add a radio button with 10 choices. I'd like to be able to set the CheckedChanged method name for each RadioButton added in c# to the OnRadioChanged method but that property isn't showing in the list. Or would I need to do something with the INotifyPropertyChanged? For this example I just want to update a label with the selected value but eventually I'll want to raise the PropertyChanged event so if there are other fields that need to update, they can listen for it.

I'm also assuming that once I figure out how to set the method, that the GroupName is included so if I have multiple radio groups added like this I can differentiate between them? If not, how can I add it? I tried adding a nested StackLayout but the GroupName property wasn't available for me to set.

Thanks

CodePudding user response:

you can't assign event handlers using the inline syntax you do for properties

try this

var radio = new RadioButton 
{ 
   Value = i.ToString(), 
   Content = string.Format("Radio text {0}", i.ToString()),
   GroupName = "RadioGroup1"
};

radio.CheckedChanged  = OnRadioChanged;

EntryFieldsStack.Children.Add(radio);
  • Related