如何通过C#获得组中选定的单选按钮?
答
<StackPanel x:Name="panel" Orientation="Vertical">
<RadioButton x:Name="1"></RadioButton>
<RadioButton x:Name="2"></RadioButton>
<RadioButton x:Name="3"></RadioButton>
<RadioButton x:Name="4"></RadioButton>
...
<RadioButton x:Name="10"></RadioButton>
</StackPanel>
for (int i = 0; i < this.panel.Children.Count; i++)
{
if (this.panel.Children[i].GetType().Name == "RadioButton")
{
RadioButton radio = (RadioButton)this.panel.Children[i];
if ((bool)radio.IsChecked)
{
this.txt.Text ="the check radio button is:"+ radio.Name.ToString();
}
}
}
选择将是价值的按钮的索引“i”对应于(布尔)radio.IsChecked是真实的,所以你可能只是记录这个值和其他地方使用它。
+0
谢谢!这是我需要的。 – SevenDays
答
甚至更容易版本
foreach (RadioButton rb in YourStackPanel.Children)
{
if (rb.IsChecked == true)
{
//Do whatever you need with it.
}
}
你的意思是你正在寻找工作再上一个单一的控制从多个单选按钮的当前选定的单选按钮? – KreepN
是的,我有。我有一个堆栈面板和RadionButtons(1到10个按钮组“acc”),并且需要选择RadioButton。 – SevenDays