locked
Is there any possible way to Bind "RadioButton" group? RRS feed

  • Question

  • Hi All,

    Can any one tell me... How to bind the Radio Button group with Boolean property? Is this achiveable ?

    Thanks and Regards,

    Sakthi

    Wednesday, December 14, 2011 6:59 AM

Answers

  • > How to bind the Radio Button group with Boolean property? Is this achiveable ?
     
     
    if i understood correctly, you want to bind two radiobuttons with a bool property.
    so below is an example.
     

     

    <Window x:Class="WpfApplication9.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication9"
            Title="MainWindow" Height="350" Width="500" FontSize="16">
        <StackPanel>
            <Button Content="Test" Click="Button_Click" HorizontalAlignment="Left" />
            <RadioButton Content="True" IsChecked="{Binding Some}" />
            <RadioButton Content="False" IsChecked="{Binding Some, Converter={local:ValueConverter}}" />
            <TextBlock Text="{Binding Some}" />
        </StackPanel>
    </Window>
    
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Markup;
    
    namespace WpfApplication9
    {
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = this;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                this.Some = !this.Some;
            }
            public event PropertyChangedEventHandler PropertyChanged = delegate { };
            public bool Some
            {
                get { return _Some; }
                set
                {
                    _Some = value;
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Some"));
                }
            }
            bool _Some;
        }
    
        public class ValueConverter : MarkupExtension, IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return !(bool)value;
            }
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return !(bool)value;
            }
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                return this;
            }
        }
    }
    
       

    p.s.
    WinForms-example is here
      
     

    • Proposed as answer by Malobukv Wednesday, December 14, 2011 5:26 PM
    • Marked as answer by Sakthi Aseer Saturday, December 17, 2011 10:28 AM
    • Edited by Malobukv Monday, January 16, 2012 10:22 AM p,s,
    Wednesday, December 14, 2011 9:04 AM

All replies

  • > How to bind the Radio Button group with Boolean property? Is this achiveable ?
     
     
    if i understood correctly, you want to bind two radiobuttons with a bool property.
    so below is an example.
     

     

    <Window x:Class="WpfApplication9.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication9"
            Title="MainWindow" Height="350" Width="500" FontSize="16">
        <StackPanel>
            <Button Content="Test" Click="Button_Click" HorizontalAlignment="Left" />
            <RadioButton Content="True" IsChecked="{Binding Some}" />
            <RadioButton Content="False" IsChecked="{Binding Some, Converter={local:ValueConverter}}" />
            <TextBlock Text="{Binding Some}" />
        </StackPanel>
    </Window>
    
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    using System.Windows.Markup;
    
    namespace WpfApplication9
    {
        public partial class MainWindow : Window, INotifyPropertyChanged
        {
            public MainWindow()
            {
                InitializeComponent();
                this.DataContext = this;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                this.Some = !this.Some;
            }
            public event PropertyChangedEventHandler PropertyChanged = delegate { };
            public bool Some
            {
                get { return _Some; }
                set
                {
                    _Some = value;
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Some"));
                }
            }
            bool _Some;
        }
    
        public class ValueConverter : MarkupExtension, IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return !(bool)value;
            }
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return !(bool)value;
            }
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                return this;
            }
        }
    }
    
       

    p.s.
    WinForms-example is here
      
     

    • Proposed as answer by Malobukv Wednesday, December 14, 2011 5:26 PM
    • Marked as answer by Sakthi Aseer Saturday, December 17, 2011 10:28 AM
    • Edited by Malobukv Monday, January 16, 2012 10:22 AM p,s,
    Wednesday, December 14, 2011 9:04 AM
  • Hi Sakthi,

    Thank you for your post.

    What Malobukv's code is great. I think it is what you need. Do you have resolved your issue as his code?

    If you have any additional quetions, please feel free to let us know.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us
    Thursday, December 15, 2011 9:31 AM