Articles in this section
Category / Section

How to notify the selection changes in Xamarin.Forms RadioButton [SfRadioButton]

1 min read

This article shows how to notify the popup by displaying the text of a selected RadioButton while changing the selection on the radio button list as follows.

 

public class Model : INotifyPropertyChanged
    {
        private bool isChecked;
 
        private ICommand stateChangedCommand;
        ..
        public ICommand StateChangedCommand
        {
            get { return stateChangedCommand; }
            set
            {
                stateChangedCommand = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("StateChangedCommand"));
            }
        }
 
        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsChecked"));
            }
        }
 
        public Model()
        {
            StateChangedCommand = new Command(OutputValue);
        }
 
        void OutputValue(object person)
        {
            if (IsChecked)
            {
                App.Current.MainPage.DisplayAlert("Message", "Selected Language", (person as SfRadioButton).Text);
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
    }
}

 

 

  <buttons:SfRadioGroup x:Name="radioGroup" BindableLayout.ItemsSource="{Binding Items}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <buttons:SfRadioButton x:Name="radioButton" IsChecked="{Binding IsChecked}" Text="{Binding Item}">
                        <buttons:SfRadioButton.Behaviors>
                            <local:EventToCommandBehavior
                                             EventName="StateChanged"
                                             Command="{Binding StateChangedCommand}"
                                             CommandParameter="{x:Reference radioButton}"/>
                        </buttons:SfRadioButton.Behaviors>
                    </buttons:SfRadioButton>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </buttons:SfRadioGroup>

 

Now, when tapped on the SfRadioButton, the corresponding command in ViewModel will be invoked automatically and the desired operation will be performed as shown in the following screenshot.

Screenshot

Xamarin.Forms SfRadioButton with MVVM Pattern

 

Download the complete sample here.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied