Click here to Skip to main content
15,794,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Experts,

I have two Radio Button(Male,Female) which i have to bind By Property (Gender). But My Gender Property Returns the Integer Value (0 For Male and 1 For Female).

So Please Suggest Me how to bind the data from database to form?

Sergey Alexandrovich Kryukov 12-Jun-12 1:19am    
How about some code sample? What did you try? What's not working?

1 solution

Greetings, eg_Anubhava;

I found that Item #358 – Binding a RadioButton to an Enumerated Type at 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"
        Title="Gender" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" SizeToContent="WidthAndHeight">
        <!-- Define a XAML Static Resource to EnumToBooleanConverter / IValueConverter -->
        <loc:EnumToBooleanConverter x:Key="EnumToBoolConverter" />

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Label Margin="6">The Person's Gender?</Label>

        <!-- RadioButton Data Bindings:                                                       -->
        <!--    Path: The SelectedGender AutoProperty inside the MainWindow class.            -->
        <!--    Converter: The Window Static Resource to the EnumToBooleanConverter           -->
        <!--    ConverterParameter: The Gender Enumerated Data Value in the Main Window Class -->
        <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>

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

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public Gender SelectedGender { get; set; }

        public MainWindow()

            // Initialize the Default Gender Value...
            SelectedGender = Gender.Male;

            // Set the Data Context to this MainWindow class for allowing
            // the Gender to be changed through Data Binding...
            this.DataContext = this;


        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,


... 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
        // Convert enum [value] to boolean, true if matches [param]
        public object Convert(object value, Type targetType, object param, CultureInfo culture)
            return value.Equals(param);

        // Convert boolean to enum, returning [param] if true
        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...
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900