Greetings, eg_Anubhava;
I found that
Item #358 – Binding a RadioButton to an Enumerated Type at
http://wpf.2000things.com/2011/08/05/358-binding-a-radiobutton-to-an-enumerated-type/ from Sean Sexton's
"2,000 Things You Should Know About WPF" to be of tremendous help. In both his example and mine below, there are a couple of key points to keep in mind when performing DataBinding to a RadioButton to WPF:
1.) You may want to consider using a Type Converter that implements the IValueConverter interface in the System.Windows.Data namespace.
2.) Don't specify a GroupName property for the RadioButton that is being bound to your porperty or data element inside your window's DataContext.
The XAML for the Window containing the RadioButtons and the DataBinding settings that use the value converter is as follows:
<Window x:Class="WPFRadioButtonBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:WPFRadioButtonBinding"
Title="Gender" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" SizeToContent="WidthAndHeight">
<Window.Resources>
<loc:EnumToBooleanConverter x:Key="EnumToBoolConverter" />
</Window.Resources>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Margin="6">The Person's Gender?</Label>
<RadioButton Name="MaleRadioButton" Margin="20, 6"
IsChecked="{Binding Path=SelectedGender, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static loc:Gender.Male}}">_Male</RadioButton>
<RadioButton Name="FemaleRadioButton" Margin="20, 6"
IsChecked="{Binding Path=SelectedGender, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static loc:Gender.Female}}">_Female</RadioButton>
<Button Name="AnswerButton" Margin="6" HorizontalAlignment="Right" Width="65" Height="25" Click="AnswerButton_Click">_Answer...</Button>
</StackPanel>
</Window>
The C# code behind for the Window would resemble the following:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WPFRadioButtonBinding
{
public enum Gender
{
Male,
Female
}
public partial class MainWindow : Window
{
public Gender SelectedGender { get; set; }
public MainWindow()
{
InitializeComponent();
SelectedGender = Gender.Male;
this.DataContext = this;
return;
}
private void AnswerButton_Click(object sender, RoutedEventArgs e)
{
string confirmGender =
string.Format("The Selected Gender Is: {0}", SelectedGender.ToString());
MessageBox.Show(confirmGender, "Selected Gender", MessageBoxButton.OK,
MessageBoxImage.Information);
return;
}
}
}
... and using Sean's
EnumToBooleanConverter to check for and perform any changes to the data bound enumerated Gender value based on which RadioButton was clicked:
using System;
using System.Globalization;
using System.Text;
using System.Windows.Data;
namespace WPFRadioButtonBinding
{
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object param, CultureInfo culture)
{
return value.Equals(param);
}
public object ConvertBack(object value, Type targetType, object param, CultureInfo culture)
{
return (bool)value ? param : Binding.DoNothing;
}
}
}
I hope this was of help and interest to you...