2011-09-08 77 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. 
      } 
     } 
相關問題