Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple WPF RadioButton Binding?

What is the simplest way to bind a group of 3 radiobuttons to a property of type int for values 1, 2, or 3?

like image 471
Johnathan1 Avatar asked Aug 23 '09 06:08

Johnathan1


People also ask

What is Radiobutton WPF?

A Radio Button is a control that allows a user to select a single option from a group of options. The user is limited to select a single option from a related list of options which are mutually exclusive. It has only two options − Selected.


2 Answers

I am very surprised nobody came up with this kind of solution to bind it against bool array. It might not be the cleanest, but it can be used very easily:

private bool[] _modeArray = new bool[] { true, false, false}; public bool[] ModeArray {     get { return _modeArray ; } } public int SelectedMode {     get { return Array.IndexOf(_modeArray, true); } } 

in XAML:

<RadioButton GroupName="Mode" IsChecked="{Binding Path=ModeArray[0], Mode=TwoWay}"/> <RadioButton GroupName="Mode" IsChecked="{Binding Path=ModeArray[1], Mode=TwoWay}"/> <RadioButton GroupName="Mode" IsChecked="{Binding Path=ModeArray[2], Mode=TwoWay}"/> 

NOTE: you don't need two-way binding if you don't want to one checked by default. TwoWay binding is the biggest con of this solution.

Pros:

  • No need for code behind
  • No need for extra class (IValue Converter)
  • No need for extra enums
  • doesn't require bizarre binding
  • straightforward and easy to understand
  • doesn't violate MVVM (heh, at least I hope so)
like image 31
wondra Avatar answered Sep 18 '22 05:09

wondra


I came up with a simple solution.

I have a model.cs class with:

private int _isSuccess; public int IsSuccess { get { return _isSuccess; } set { _isSuccess = value; } } 

I have Window1.xaml.cs file with DataContext set to model.cs. The xaml contains the radiobuttons:

<RadioButton IsChecked="{Binding Path=IsSuccess, Converter={StaticResource radioBoolToIntConverter}, ConverterParameter=1}" Content="one" /> <RadioButton IsChecked="{Binding Path=IsSuccess, Converter={StaticResource radioBoolToIntConverter}, ConverterParameter=2}" Content="two" /> <RadioButton IsChecked="{Binding Path=IsSuccess, Converter={StaticResource radioBoolToIntConverter}, ConverterParameter=3}" Content="three" /> 

Here is my converter:

public class RadioBoolToIntConverter : IValueConverter {     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)     {         int integer = (int)value;         if (integer==int.Parse(parameter.ToString()))             return true;         else             return false;     }      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)     {         return parameter;     } } 

And of course, in Window1's resources:

<Window.Resources>     <local:RadioBoolToIntConverter x:Key="radioBoolToIntConverter" /> </Window.Resources> 
like image 180
Johnathan1 Avatar answered Sep 22 '22 05:09

Johnathan1