WPF radio button checking
Asked Answered
J

2

7

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
            }
Jaimiejain answered 20/10, 2013 at 13:49 Comment(0)
S
13

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)
{
}
Scrawly answered 20/10, 2013 at 13:51 Comment(0)
R
4

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?.

Ruscio answered 20/10, 2013 at 13:53 Comment(1)
You wouldn't have to explain your code if you'd written if(RBMetLines.HasValue && RBMetLines.Value) insteadKist

© 2022 - 2024 — McMap. All rights reserved.