Trying to bind RadioButtonGroup.SelectedValue to a vaiable....

Carlo Goretti 121 Reputation points
2021-06-12T19:53:40.423+00:00

Hey, im trying to bind RadioButtonGroup.SelectedValue to a vaiable but dosent get it to work... Can someone see what im doing wrong here?
Here is my XAML Code:
<StackLayout RadioButtonGroup.GroupName="TrainingStyleRB"
RadioButtonGroup.SelectedValue="{Binding SelectedRadioButton}">

And here is my Code behind:

object SelectedRadioButton;

             var test2 = (RadioButton) SelectedRadioButton;
                var test3 = test2.Value;

Thankful for some tips!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,362 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kyle Wang 5,531 Reputation points Microsoft Vendor
    2021-06-14T02:17:04.27+00:00

    Hi CarloGoretti-1185,

    Welcome to our Microsoft Q&A platform!

    The SelectedValue property is updated to the Value property of the checked RadioButton.

    Here is a demo you can refer to.

    XAML:

    <StackLayout RadioButtonGroup.GroupName="TrainingStyleRB"   
                     RadioButtonGroup.SelectedValue="{Binding Color}">  
        <Label Text="{Binding Color, StringFormat='The Selection is {0}'}"/>  
      
        <RadioButton Content="Red"  
                         Value="Red"/>  
        <RadioButton Content="Green"  
                         Value="Green"/>  
        <RadioButton Content="Blue"  
                         Value="Blue"/>  
        <RadioButton Content="Other"  
                         Value="Other"/>  
    </StackLayout>  
    

    ViewModel:

    class MainPageViewModel : INotifyPropertyChanged  
    {  
        string color;  
        public string Color  
        {  
            get => color;  
            set  
            {  
                color = value;  
                OnPropertyChanged("Color");  
            }  
        }  
      
        public event PropertyChangedEventHandler PropertyChanged;  
        protected void OnPropertyChanged(string propertyName)  
        {  
            var handler = PropertyChanged;  
            if (handler != null)  
                handler(this, new PropertyChangedEventArgs(propertyName));  
        }  
      
        public MainPageViewModel()  
        {  
            Color = "Red";  
        }  
    }  
    

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.