如何一次只检查一个单选按钮(其他禁用)



我在silverlight c#工作,我有我以编程方式创建单选按钮的情况。我的代码是这样做的:

 Grid childGrid = CreateChildGrid(); 
 int NumberOfRadioButton =0;
 RadioButton[] RadioBut= new RadioButton[5];
 int count = 0;
 foreach (var item in param.Component.Attributes.Items)// this  param.Component.Attributes.Items value is 4 in fact.
 {                 
     NumberOfRadioButton++;
     RadioBut[count] = new RadioButton();
     RadioBut[count].GroupName = item;
     RadioBut[count].Content = item;
     sp.Children.Add(RadioBut[count]);
     count++;
 }

问题:问题是它检查所有的按钮,因为我想一次只检查一个按钮。我的意思是,如果一个被选中,其他的一定是禁用的。

有人能帮我实现我的目标吗?非常感谢。

注意:我使用silverlight这样做

您应该将所有RadioButtonsGroupName属性设置为相同,因此它们将被"分组",这意味着一次只能选择其中一个。

所以这一行:

rbs[count].GroupName = item;

应该是这样的:

rbs[count].GroupName = "MyRadioButtonGroup";

当然,字符串可以是任何东西,只要它是相同的所有RadioButtons

您必须将这些单选按钮分配到一个公共组。检查单选按钮组名称

RadioButton[] rbs = new RadioButton[5];
rbs.GroupName = "Add your common groupname here"; // added code
 int count = 0;

相关内容

最新更新