在后面的代码中创建单选按钮时如何获取IsChecked? [英] How to get IsChecked when creating Radiobuttons in code behind?

查看:225
本文介绍了在后面的代码中创建单选按钮时如何获取IsChecked?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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.

推荐答案

有两种通用的处理方式,具体取决于您是否使用MVVM模式.如果您不使用MVVM,则可以使用OnChecked 事件,该事件包括一个sender参数.输入的值可用于确定已更改的单选按钮:

There are two general ways of handling this depending on wether you are using MVVM pattern or not. If you are not using MVVM, then you can use the OnChecked event which includes a sender argument. The value entered can be used to determine the radio button that was changed:

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.


您可以编写如下代码:
You can write the code like:
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[^]


在xaml中创建了一个用户控件,这使得在选中或不选中单选按钮时都可以轻松设置变量.该变量已发送至我的代码以供使用.
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.


这篇关于在后面的代码中创建单选按钮时如何获取IsChecked?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆