Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of the checked radiobutton in wpf

I have four RadioButtons in a grid panel, but when I do this:

<GroupBox x:Name="radioButtons">
    <RadioButton Content="1" Height="16" HorizontalAlignment="Left" Margin="10,45,0,0" Name="status1" VerticalAlignment="Top" />
    <RadioButton Content="2" Height="16" HorizontalAlignment="Left" Margin="10,67,0,0" Name="status2" VerticalAlignment="Top" />
    <RadioButton Content="3" Height="16" HorizontalAlignment="Left" Margin="10,89,0,0" Name="status3" VerticalAlignment="Top" />
    <RadioButton Content="4" Height="16" HorizontalAlignment="Left" Margin="10,111,0,0" Name="status4" VerticalAlignment="Top" />
</GroupBox>

It says that:

Error 1 The object 'GroupBox' already has a child and cannot add 'RadioButton'. 'GroupBox' can accept only one child.

And the last three RadioButtons say:

The property 'Content' is set more than once.

What's wrong with my GroupBox? Furthermore, in my code I want to access the RadioButton that is checked (preferably as an int). How do I do this? I tried to look in Google and I found a lot of results, but I couldn't understand any of them.

like image 250
DorZ11 Avatar asked Apr 16 '13 12:04

DorZ11


1 Answers

GroupBox can only hold 1 item, hence the error about trying to set the Content property of the GroupBox multiple times.

So, make that a Layout item and then put the RadioButtons inside it. Now, you set the Content once, which is the StackPanel, and that Layout item can hold many children -> RadioButtons.

<GroupBox x:Name="radioButtons">
  <StackPanel>
    <RadioButton Name="status1"
                  Height="16"
                  Margin="10,45,0,0"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  Content="1" />
    <RadioButton Name="status2"
                  Height="16"
                  Margin="10,67,0,0"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  Content="2" />
    <RadioButton Name="status3"
                  Height="16"
                  Margin="10,89,0,0"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  Content="3" />
    <RadioButton Name="status4"
                  Height="16"
                  Margin="10,111,0,0"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  Content="4" />
  </StackPanel>
</GroupBox>

As for your second question, Christian Mosers WPF Tutorial.net has a decent sample. If you don't understand it, you maybe should look at the topics Binding and Converter, first.

A very crude way to be notified of RadioButton checked in a non MVVM way:

private void RadioButtonChecked(object sender, RoutedEventArgs e) {
  var radioButton = sender as RadioButton;
  if (radioButton == null)
    return;
  int intIndex = Convert.ToInt32(radioButton.Content.ToString());
  MessageBox.Show(intIndex.ToString(CultureInfo.InvariantCulture));
}

Then, in each of your RadioButtons in xaml, add Checked="RadioButtonChecked".

like image 116
Viv Avatar answered Sep 28 '22 19:09

Viv