Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Mobile WPF wpftoolkit
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?
 
Thanks
Posted 11-Jun-12 19:59pm
Comments
SAKryukov at 12-Jun-12 1:19am
   
How about some code sample? What did you try? What's not working?
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
        <!-- Define a XAML Static Resource to EnumToBooleanConverter / IValueConverter -->
        <loc:EnumToBooleanConverter x:Key="EnumToBoolConverter" />
    </Window.Resources>
 
    <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>
    </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
    }
 
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public Gender SelectedGender { get; set; }
 
        public MainWindow()
        {
            InitializeComponent();
 
            //
            // 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;
 
            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
    {
        // 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...
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 244
3 BillWoodruff 210
4 Maciej Los 192
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 21 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100