C# 如何获取wpf中选中单选按钮的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16036469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:47:18  来源:igfitidea点击:

How to get the value of the checked radiobutton in wpf

c#wpfradio-buttongridpanel

提问by DorZ11

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

RadioButtons在网格面板中有四个,但是当我这样做时:

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

错误 1 ​​对象“GroupBox”已经有一个子项,无法添加“RadioButton”。“GroupBox”只能接受一个孩子。

And the last three RadioButtonssay:

最后三个RadioButtons说:

The property 'Content' is set more than once.

属性“内容”设置了不止一次。

What's wrong with my GroupBox? Furthermore, in my code I want to access the RadioButtonthat 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.

我的GroupBox怎么了?此外,在我的代码中,我想访问RadioButton被检查的(最好是int)。我该怎么做呢?我试着在谷歌上搜索,发现了很多结果,但我一个都看不懂。

采纳答案by Viv

GroupBoxcan only hold 1 item, hence the error about trying to set the Contentproperty of the GroupBoxmultiple times.

GroupBox只能容纳 1 个项目,因此关于尝试多次设置Content属性的错误GroupBox

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

因此,将其设为 Layout 项,然后将RadioButtons 放入其中。现在,您设置了Content一次,即StackPanel,并且该 Layout 项可以容纳许多子项 -> 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.nethas a decent sample. If you don't understand it, you maybe should look at the topics Bindingand Converter, first.

至于你的第二个问题,Christian Mosers WPF Tutorial.net有一个不错的样本。如果你不明白,你也许应该先看看主题BindingConverter

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

RadioButton以非 MVVM 方式通知检查的一种非常粗略的方式:

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

然后,在RadioButtonxaml中的每个s 中,添加Checked="RadioButtonChecked".

回答by Tan

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

如果您需要许多元素或控件,则必须将它们放在布局容器中。

  • Grid
  • StackPanel
  • DockPanel
  • WrapPanel
  • Etc.....
  • 网格
  • 堆栈面板
  • 码头面板
  • 包裹面板
  • 等等.....