How to get the value of the checked radiobutton in wpf
Asked Answered
S

2

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.

Sfumato answered 16/4, 2013 at 12:0 Comment(0)
G
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".

Garris answered 16/4, 2013 at 12:3 Comment(8)
thanks it really helped me! can u direct me how to get who is checked into the code?Sfumato
I added a link to the last part of my answer. wpftutorial.net/RadioButton.html is a pretty decent sample of achieving what you need.Garris
added a function tied into Checked event of RadioButton. intIndex should be an int value provided the Content of RadioButton is set accordinglyGarris
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: <Window.DataContext> <local:MainWindowViewModel x:Name="mainWindowVM"/> </Window.DataContext> and my MainWindowViewModel allready inherit DependencyObject so I cant inherit IValueConverter..Sfumato
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 checkedGarris
at the code: <Window.Resources> <EnumMatchToBooleanConverter x:Key="enumConverter" /> </Window.Resources> I get the next error: Error 1 The tag 'EnumMatchToBooleanConverter' does not exist in XML namespace 'schemas.microsoft.com/winfx/2006/xaml/presentation'. I did a class in the name EnumMatchToBooleanConverter ..Sfumato
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 inGarris
"A very crude way to be notified of RadioButton checked in a non MVVM way" - what's the MVVM way?Sheol
G
0

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

  • Grid
  • StackPanel
  • DockPanel
  • WrapPanel
  • Etc.....
Ghiberti answered 16/4, 2013 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.