Determining checked Radiobutton from groupbox in WPF following MVVM Determining checked Radiobutton from groupbox in WPF following MVVM wpf wpf

Determining checked Radiobutton from groupbox in WPF following MVVM


you can bind RadioButton.Command of Radiobuttons to a command of your ViewModel and send a unique CommandParameter to identify which button has called the command in commandhandler.

<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio1"/><RadioButton Command="{Binding MyCommand}" CommandParameter="Radio2"/><RadioButton Command="{Binding MyCommand}" CommandParameter="Radio3"/>

in command handler check for parameter to identify the radiobutton.

Thanks


You can create an enum that contains the values of the RadioButton objects as names (roughly) and then bind the IsChecked property to a property of the type of this enum using an EnumToBoolConverter.

public enum Options{    All, Current, Range}

Then in your view model or code behind:

private Options options = Options.All; // set your default value herepublic Options Options{     get { return options; }    set { options = value; NotifyPropertyChanged("Options"); }}

Add the Converter:

[ValueConversion(typeof(Enum), typeof(bool))]public class EnumToBoolConverter : IValueConverter{    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)    {        if (value == null || parameter == null) return false;        string enumValue = value.ToString();        string targetValue = parameter.ToString();        bool outputValue = enumValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase);        return outputValue;    }    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)    {        if (value == null || parameter == null) return null;        bool useValue = (bool)value;        string targetValue = parameter.ToString();        if (useValue) return Enum.Parse(targetType, targetValue);        return null;    }}

Then finally, add the bindings in the UI, setting the appropriate ConverterParameter:

<RadioButton Content="All Pages" IsChecked="{Binding Options, Converter={    StaticResource EnumToBoolConverter}, ConverterParameter=All}" /><RadioButton Content="Current Page" IsChecked="{Binding Options, Converter={    StaticResource EnumToBoolConverter}, ConverterParameter=Current}" /><RadioButton Content="Page Range" IsChecked="{Binding Options, Converter={    StaticResource EnumToBoolConverter}, ConverterParameter=Range}" />

Now you can tell which is set by looking at the Options variable in your view model or code behind. You'll also be able to set the checked RadioButton by setting the Options property.


There is another MVVM way to solve this using IsChecked Property

Here's the XAML

<Page><Page.Resources><DataTemplate x:Key="ChoiceItemTemplate"><RadioButton Content="{Binding individualRadioButtonText}"     IsTabStop="True"     GroupName="choice"     IsChecked="{Binding IsChecked, Mode=TwoWay}"/> </DataTemplate></Page.Resources> <StackPanel>  <TextBlock Text="{Binding ChoiceQuestion}" /> <ItemsControl  ItemsSource="{Binding ListOfAnswerOptions}"                ItemTemplate="{StaticResource ChoiceItemTemplate}" /> </StackPanel></Page>

Your model will be something like this

 public class RadioButtonQuestion {    public string ChoiceQuestion { get; set; }    public string answer { get; set; }    public List<AnswerOption> ListOfAnswerOptions { get; set; } } public class AnswerOption {    public string individualRadioButtonText { get; set; }    public bool IsChecked { get; set; } }

ViewModel will look something like this (The selection logic)

RadioButtonQuestion r = new RadioButtonQuestion();var selectedElement = rbuttonQuestion.answerOptions.FirstOrDefault(c => c.IsChecked);r.answer = selectedElement.individualRadioButtonText;

So if you set the datacontext of the view to this viewmodel. You must be able to get it to work.

Hope it helps.