2011-09-08 47 views
0

如何通过c#获取位于面板(父容器)内的选定Radibutton控件的索引?如何通过C#获得组中选定的单选按钮?

如果解决方案需要,Radibuttons的控制被命名为“acc”。

感谢

+0

你的意思是你正在寻找工作再上一个单一的控制从多个单选按钮的当前选定的单选按钮? – KreepN

+0

是的,我有。我有一个堆栈面板和RadionButtons(1到10个按钮组“acc”),并且需要选择RadioButton。 – SevenDays

回答

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

4

甚至更​​容易版本

 foreach (RadioButton rb in YourStackPanel.Children) 
     { 
      if (rb.IsChecked == true) 
      { 
       //Do whatever you need with it. 
      } 
     } 
相关问题