Home>

Currently, I am making a WPF system in C #.
Therefore, I would like to know how to install a radio button and a button, get the value from the radio button when the button is pressed, and put it in the check state when the screen is displayed. I checked it myself and tried it, but it didn't work. Thank you.

// I want to get which radio button is pressed, male or female.
<StackPanel>
          <RadioButton Content = "Man" HorizontalAlignment = "Left" Margin = "390,155,0,0" VerticalAlignment = "Top" Checked = "man_Checked" />
          <RadioButton Content = "Woman" HorizontalAlignment = "Left" Margin = "390,5,0,0" VerticalAlignment = "Top" Checked = "woman_Checked" />
      </StackPanel>
// I want to get which radio button is pressed, college graduate or high school graduate.
      <StackPanel>
          <RadioButton Content = "University Graduate" HorizontalAlignment = "Left" Margin = "210,155,0,0" VerticalAlignment = "Top" RenderTransformOrigin = "-1.387,0.2" Checked = "daisotsu_Checked" />
          <RadioButton Content = "High School Graduate" HorizontalAlignment = "Left" Margin = "210,5,0,0" VerticalAlignment = "Top" IsChecked = "False" Checked = "kousotsu_Checked" />
      </StackPanel>
//button
      <Button Content = "Change" HorizontalAlignment = "Left" Margin = "660,181,0,0" VerticalAlignment = "Top" Width = "75" Click = "Button_Click" />
What I tried

I tried this method myself to get the value, but I don't know the initial value setting.
Also, I would like to know if there is a better way to acquire it.

  private void man_Checked (object sender, RoutedEventArgs e)
        {Sex = "man";}
        private void woman_Checked (object sender, RoutedEventArgs e)
        {Sex = "woman";}
        public string sex
        {set;get;}
        private void daisotsu_Checked (object sender, RoutedEventArgs e)
        {Gakureki = "university graduate";}
        private void kousotsu_Checked (object sender, RoutedEventArgs e)
        {Gakureki = "High school graduate";}
        public string Gakureki
        {set;get;}

Supplementary information (FW/tool version, etc.)
Software: visual studio 2019

  • Answer # 1

    If you name the radio button with Name, you can refer to the property in the code behind.

    <RadioButton x: Name = "Man" Content = "Man" />
    MessageBox.Show (Man.IsChecked.ToString ());

  • Answer # 2

    Get

    <RadioButton x: Name = "Man" Content = "Man" />
    MessageBox.Show (Man.Content.ToString ());


    I got it by writing.

    Checking the radio buttons

    Man.IsChecked = true;


    I was able to check the radio button of the man.