question

CarloGoretti-1185 avatar image
0 Votes"
CarloGoretti-1185 asked KyleWang-MSFT edited

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

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!







dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

KyleWang-MSFT avatar image
0 Votes"
KyleWang-MSFT answered KyleWang-MSFT edited

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.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hey, Do i need to have a ViewModel or can i make this in the code Behind of the View? anonymous userWang-MSFT

0 Votes 0 ·

@CarloGoretti-1185 Of course you can edit in xaml.cs. But I think using ViewModel is a better choice.

0 Votes 0 ·

Do i need all this?

  public event PropertyChangedEventHandler PropertyChanged;
      protected void OnPropertyChanged(string propertyName)
      {
          var handler = PropertyChanged;
          if (handler != null)
              handler(this, new PropertyChangedEventArgs(propertyName));
      }

I have done it like this:

 string selectedRadioButton;
    
 //string color;
 public string SelectedRadioButton
 {
     get => selectedRadioButton;
     set
     {
         selectedRadioButton = value;
         //OnPropertyChanged("Color");
     }
 }

But dosent get it to work...
anonymous userWang-MSFT





0 Votes 0 ·
Show more comments