6

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.

hopper
  • 13,060
  • 7
  • 49
  • 53
DorZ11
  • 85
  • 1
  • 1
  • 6

2 Answers2

6

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

M-Pixel
  • 3,501
  • 2
  • 16
  • 27
Viv
  • 17,170
  • 4
  • 51
  • 71
  • thanks it really helped me! can u direct me how to get who is checked into the code? – DorZ11 Apr 16 '13 at 12:14
  • I added a link to the last part of my answer. http://wpftutorial.net/RadioButton.html is a pretty decent sample of achieving what you need. – Viv Apr 16 '13 at 12:15
  • added a function tied into Checked event of RadioButton. intIndex should be an int value provided the Content of RadioButton is set accordingly – Viv Apr 16 '13 at 12:29
  • first of all thanks for ur help! secondly, I dont know how to bind it to my main code because I connect my xaml to my main with this code: and my MainWindowViewModel allready inherit DependencyObject so I cant inherit IValueConverter.. – DorZ11 Apr 16 '13 at 12:36
  • You dont make your ViewModel implement the IValueConverter. Create a seperate class for it. Exactly like how the example shows in that link called "EnumMatchToBooleanConverter". <- is a seperate class!!! Now in your MainWindowViewModel you will have a Property called "CurrentOption" of string type. This will get the string set to whatever is in the corresponding RadioButton's CommandParameter when checked – Viv Apr 16 '13 at 12:43
  • at the code: I get the next error: Error 1 The tag 'EnumMatchToBooleanConverter' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. I did a class in the name EnumMatchToBooleanConverter .. – DorZ11 Apr 16 '13 at 13:51
  • well thats because you need to provide the namespace under which you have that class. in your Window do you have a xmlns:local... ? you need to provide the namespace the converter class is declared in – Viv Apr 16 '13 at 13:54
  • "A very crude way to be notified of RadioButton checked in a non MVVM way" - what's the MVVM way? – Maverick Meerkat Jun 19 '17 at 08:30
0

If you want many elements or controls then you have to put them in layout containers.

  • Grid
  • StackPanel
  • DockPanel
  • WrapPanel
  • Etc.....
Tan
  • 2,148
  • 3
  • 33
  • 54