0
votes

Just to be clear I'm talking here about the RadioButton released with Xamarin 4.6 in 2006.

My aim here is to create one binding which will work with multiple RadioButtons in the same group. Ultimately so I can leave my screen and come back to it and set the last selected RadioButton item.

So I know you can set a true of false property binding to the IsChecked property. However, this single item has no context of which item it is.

I know you can also set a binding context to a page or a control... so I guess I could set a context to a StackLayout for all the items inside it... but this still won't help.

This is something like what I want to achieve... yes I won't acctually have 10, I'm just making a point that I don't want to have to use lots of different bindings..

<RadioButton GroupName="Numbers" Text="One" 
  IsChecked="{Binding IsCheckedNumbersgroup}" />

<RadioButton GroupName="Numbers" Text="Two" 
  IsChecked="{Binding IsCheckedNumbersgroup}" />
....
<RadioButton GroupName="Numbers" Text="Ten" 
  IsChecked="{Binding IsCheckedNumbersgroup}" />
1

1 Answers

2
votes

If we binding the same source with multi RadioButton , it will cause issue as when you click one of them , other RadioButton will also been effected .

So in your case if you do not want to define multi property , you could define a List and binding them with the item .

in Xaml

Command, which is executed when the RadioButton is selected.

<RadioButton GroupName="Numbers" Text="One" IsChecked="{Binding MySource[0]}" Command="{Binding ButtonCommand}" CommandParameter="0" />

<RadioButton GroupName="Numbers" Text="Two" IsChecked="{Binding MySource[1]}" Command="{Binding ButtonCommand}" CommandParameter="1" />

<RadioButton GroupName="Numbers" Text="Three" IsChecked="{Binding MySource[2]}" Command="{Binding ButtonCommand}" CommandParameter="2" />

in ViewModel

public ObservableCollection<bool> MySource { get; set; }
public xxxViewModel()
        {
            MySource = new ObservableCollection<bool>() {true,false,false };

            ButtonCommand = new Command((org)=> {

 
                var index = int.Parse(org.ToString());

                App.Current.MainPage.DisplayAlert("Title","the status of RadioButton"+(index+1).ToString()+"is"+MySource[index].ToString()  ,"OK");

            });

        }