问题描述
我是Silverlight&的新手. C#,我需要以下帮助:
从XML数据中,我将根据需要创建尽可能多的RadioButton(与root.nodes一样多),如下所示:
I''m very new to Silverlight & C# and I need help with following:
From XML data I''m creating as many RadioButtons as needed (as many as there are root.nodes) as following:
foreach (var vItem in vXDocument.Descendants("kaart"))
{
vItemsCount++;
// MC1
RadioButton vRadioButton_MC1 = new RadioButton();
vRadioButton_MC1.Content = vItem.Element("MC1").Value;
vRadioButton_MC1.Name = vItemsCount + "-MC1";
vListBox.Items.Add(vRadioButton_MC1);
// MC2
RadioButton vRadioButton_MC2 = new RadioButton();
vRadioButton_MC2.Content = vItem.Element("MC2").Value;
vRadioButton_MC2.Name = vItemsCount + "-MC2";
vListBox.Items.Add(vRadioButton_MC2);
};
现在,我要检查是否已选中RadioButtons,而没有选中.每个对象都有自己的指定名称(vRadioButton_MC1.Name = vItemsCount +"-MC1").我可以向每个RadioButton询问此属性吗?
感谢您的关注.
[edit] OP的其他解决方案中的其他信息已移至此处[/edit]
在xaml中添加了一个用户控件,这使得在选中或不选中单选按钮时都可以轻松设置变量.该变量已发送至我的代码以供使用.
Now I want to check with RadioButtons are checked and with aren''t. Every object has his own specified name (vRadioButton_MC1.Name = vItemsCount + "-MC1"). Can I ask this property for each RadioButtons?
Thanks for paying attention.
[edit]Additional information by OP in a non-solution moved here [/edit]
made an user control in xaml, wich made it easy to set a variable when a radiobutton is checked or not. That variable i''ve send to my code behind to work with.
推荐答案
private void OnChecked(object sender, RoutedEventArgs e)
{
var control = (RadioButton)sender;
}
要使用此功能,您需要将事件添加到控件中:
To use this you need to add the event to the control:
vRadioButton_MC1.Checked += OnChecked;
更好的方法是使用MVVM模式,但这将更加复杂,因为您必须在代码中创建ICommand
绑定.将使用CommandParameter
确定激活了哪个RadioButton.
The better way to do this is to use the MVVM pattern, but that will be much more complex since you have to create a ICommand
binding in code. The CommandParameter
would be u8sed to determine which RadioButton was activated.
vRadioButton.IsChecked
请参见 http中的IsChecked属性. ://msdn.microsoft.com/zh-cn/library/system.windows.controls.primitives.togglebutton.ischecked%28v=VS.95%29.aspx [ ^ ]
IsChecked property is refereed at http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.ischecked%28v=VS.95%29.aspx[^]
这篇关于在后面的代码中创建单选按钮时如何获取IsChecked?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!