locked
How to select radio button programatically [i.e. from code] in WPF? RRS feed

  • Question

  • I would like to select/deselect radio buttons in WPF using code. I looked around on the forum but didn't find a right solution. Any help will be useful. Thanks a lot in advance!
    Friday, February 15, 2013 12:04 AM

Answers

  • Found the solution which I overlooked before. Really simple. Just need to change the IsChecked property.

    <Window x:Class="RadioButtonSelect.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="200" Width="200"><Grid><RadioButton x:Name="StateARadioButton" Margin="97,22,74,0" VerticalAlignment="Top" BorderThickness="2" GroupName="MyStates" Checked="OfflineRadioButton_Checked"/><RadioButton x:Name="StateBRadioButton" Margin="97,82,74,0" VerticalAlignment="Top" BorderThickness="2" GroupName="MyStates" Checked="OnlineRadioButton_Checked"/><Label x:Name="StateARadioButtonLabel" Content="StateA" HorizontalAlignment="Left" Margin="25,22,0,0" VerticalAlignment="Top"/><Label x:Name="StateBRadioButtonLabel" Content="StateB" HorizontalAlignment="Left" Margin="25,72,0,0" VerticalAlignment="Top"/></Grid></Window>

    using System.Windows;
     
    namespace RadioButtonSelect
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                changeToStateA();
                changeToStateB();
            }
     
            public void changeToStateA()
            {
                StateARadioButton.IsChecked = true;
            }
     
            public void changeToStateB()
            {
                StateBRadioButton.IsChecked = true;
            }
     
            private void OfflineRadioButton_Checked(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("State A selected");
            }
     
            private void OnlineRadioButton_Checked(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("State B selected");
            }
        }
    }
                                                                         
    • Marked as answer by SuperWebSurfer Friday, February 15, 2013 1:53 AM
    Friday, February 15, 2013 1:53 AM

All replies

  • Found the solution which I overlooked before. Really simple. Just need to change the IsChecked property.

    <Window x:Class="RadioButtonSelect.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="200" Width="200"><Grid><RadioButton x:Name="StateARadioButton" Margin="97,22,74,0" VerticalAlignment="Top" BorderThickness="2" GroupName="MyStates" Checked="OfflineRadioButton_Checked"/><RadioButton x:Name="StateBRadioButton" Margin="97,82,74,0" VerticalAlignment="Top" BorderThickness="2" GroupName="MyStates" Checked="OnlineRadioButton_Checked"/><Label x:Name="StateARadioButtonLabel" Content="StateA" HorizontalAlignment="Left" Margin="25,22,0,0" VerticalAlignment="Top"/><Label x:Name="StateBRadioButtonLabel" Content="StateB" HorizontalAlignment="Left" Margin="25,72,0,0" VerticalAlignment="Top"/></Grid></Window>

    using System.Windows;
     
    namespace RadioButtonSelect
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                changeToStateA();
                changeToStateB();
            }
     
            public void changeToStateA()
            {
                StateARadioButton.IsChecked = true;
            }
     
            public void changeToStateB()
            {
                StateBRadioButton.IsChecked = true;
            }
     
            private void OfflineRadioButton_Checked(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("State A selected");
            }
     
            private void OnlineRadioButton_Checked(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("State B selected");
            }
        }
    }
                                                                         
    • Marked as answer by SuperWebSurfer Friday, February 15, 2013 1:53 AM
    Friday, February 15, 2013 1:53 AM
  • Good job, SilentKilerr100!

    Glad to know you have found the solution and thanks for sharing it with us.

    Have a nice day!

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 15, 2013 10:34 AM
  • That way requires alot more code than you need as you will then need to read the values of them after etc.  the easiest way would be to bind the isChecked property to a property in your view model, then whenever that property changes the check box will change state, assuming that your viewmodel implements INotifyPropertyChanged.

    andy

    Friday, February 15, 2013 11:46 AM