WPF radio button checking

40,954

Solution 1

Give x:Name or Name to your RadioButton like

<RadioButton x:Name="MyRadioButton" Content="Metinės"/>

and then in code behind you can check

if(MyRadioButton.IsChecked == true)
{
}

Solution 2

You can find out like this

Give your Radio Button name using x:Name ="RBMetLines" and access that in code behind

<RadioButton Content="Metinės"
             x:Name="RBMetLines"
             Checked="RBMetLines_Checked"
             HorizontalAlignment="Left"
             Margin="393,124,0,0"
             Height="21"
             Width="101"
             FontSize="14"
             ClickMode="Press"
             VerticalAlignment="Top"
             FontFamily="Segoe WP Semibold"/>

and in C# code behind

private void RBMetLines_Checked(object sender, RoutedEventArgs e)
{
    if(Convert.ToBoolean(RBMetLines.IsChecked))
    {
        //program code
    }
}

I have converted IsChecked to Boolean because in WPF IsChecked is bool?.

Share:
40,954
pauliustack
Author by

pauliustack

Updated on October 21, 2020

Comments

  • pauliustack
    pauliustack over 3 years

    Now im coding my first gui program and i have a problem(I know its very simple,but i cant find an answer for it).I have 2 radio buttons,separeted from eachother,and i cant check if radio button is checked ,here`s my code:

     <RadioButton Content="Metinės"
                     Checked="RadioButton_Checked_1"
                     HorizontalAlignment="Left"
                     Margin="393,124,0,0"
                     Height="21"
                     Width="101"
                     FontSize="14"
                     ClickMode="Press"
                     VerticalAlignment="Top"
                     FontFamily="Segoe WP Semibold"/>
    

    And c#

      if (RadioButton_Checked == true)
                {
                    //program code
                }
    
  • Trajan
    Trajan about 9 years
    You wouldn't have to explain your code if you'd written if(RBMetLines.HasValue && RBMetLines.Value) instead