I have a groupbox with some radiobuttons. How do I get to know which one which is checked? I am using WPF and following MVVM.
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 here
public 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.
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
If you use Tag property on options buttons (boolean, integer, strings) like in his XAML
<StackPanel Orientation="Horizontal" Margin="10,10, 0, 0">
<RadioButton Name="rP0" Content="Low " Tag="0" />
<RadioButton Name="rP1" Content="Normal" Tag="1" IsChecked="True" />
<RadioButton Name="rP2" Content="Medium" Tag="2" />
<RadioButton Name="rP3" Content="High" Tag="3" />
</StackPanel>
Then you can use following function to get selected value (button)
int priority = SelectedRadioValue<int>(0, rP0, rP1, rP2, rP3);
where
public T SelectedRadioValue<T>(T defaultValue, params RadioButton[] buttons)
{
foreach (RadioButton button in buttons)
{
if (button.IsChecked == true)
{
if (button.Tag is string && typeof(T) != typeof(string))
{
string value = (string) button.Tag;
return (T) Convert.ChangeType(value, typeof(T));
}
return (T)button.Tag;
}
}
return defaultValue;
}
I got same problem and i solved by removing "GroupName" Property from Radiobutton.
Please remove "GroupName" Property from all radio buttons. and check