简体   繁体   English

如何知道所选单选按钮的索引

[英]How to know the index of radiobutton selected

How to know the selcted index of a radio button to know which index option is selcted by user. 如何知道单选按钮的选择索引,以了解用户选择了哪个索引选项。

Currently my code displays the items read. 目前,我的代码显示已读取的项目。 But when i select the items appeared then it shows in a textblock the selected item of radiobutton (of the given 3 options, because my radiobutton currently has 3 items). 但是,当我选择出现的项目时,它会在文本块中显示单选按钮的所选项目(给定3个选项,因为我的单选按钮当前有3个项目)。

My try fo this is : 我的尝试是:

foreach (String item in param.Parameter[lop].Component.Attributes.Items)
{
    RadioButton radio = new RadioButton()
    {
        Content = item,
        GroupName = "MyRadioButtonGroup"
    };
    radio.Checked += (o, e) =>
    {
        txtblkShowStatus.Text = item;       
    };
    data= param.Parameter[lop].Label;
    sp.Children.Add(radio);                           
}

Now i want something like insted of "item" based selection now i want index based seelction. 现在我想要类似“基于项目”的选择,现在我想要基于索引的渗透。 I want some thing like . 我想要类似的东西。 If user select 2nd button(of radiobutton) then i will display some UI element. 如果用户选择第二个按钮(单选按钮),那么我将显示一些UI元素。 And the items are obtained on deserializing an xml. 这些项目是在反序列化xml时获得的。 Which only deals with the index of the radio buttons. 仅处理单选按钮的索引。 So i want something like if(selectedIndexofRadioButton ==IndexObtainedFromXmlInIntegr){do something} 所以我想要类似if(selectedIndexofRadioButton ==IndexObtainedFromXmlInIntegr){do something}

Is it possible to do? 有可能吗? If yes then how should i change my code to achieve this ? 如果是,那我应该如何更改我的代码来实现呢? EDIT: My code change after DonBoitnott's answer edit is : 编辑:DonBoitnott的答案编辑后,我的代码更改为:

foreach (String item in param.Parameter[lop].Component.Attributes.Items)
                        {
                            RadioButton radio = new RadioButton()
                            {
                                Content = item,
                                GroupName = "MyRadioButtonGroup",
                                Tag=tg
                            };
                            radio.Checked += (o, e) =>
                            {
                                txtblkShowStatus.Text = item;
                                if (((Int32)((RadioButton)o).Tag).Equals(2))
                                {
                                    MessageBox.Show("hurrey");
                                }
                            };
                            radio.Tag=1;
                            data= param.Parameter[lop].Label;
                            sp.Children.Add(radio);
                            index++; tg++;
                        }

You should leverage the Tag property of the RadioButton control object: 您应该利用RadioButton控件对象的Tag属性:

foreach (String item in param.Parameter[lop].Component.Attributes.Items)
{
    RadioButton radio = new RadioButton()
    {
        Content = item,
        GroupName = "MyRadioButtonGroup",
        Tag = //integer value from XML
    };
    radio.Checked += (o, e) =>
    {
        txtblkShowStatus.Text = item;       
    };
    data = param.Parameter[lop].Label;
    sp.Children.Add(radio);                           
}

The Tag property is an Object , so you can assign anything to it. Tag属性是一个Object ,因此您可以为其分配任何内容。 You just have to cast it on the way out when you use it: 您只需要在使用它时将其抛弃即可:

if (((Int32)radioButton.Tag).Equals(IndexObtainedFromXmlInInteger)
{
    //do something
}

In response to your comment, OP: 针对您的评论,OP:

radio.Checked += (o, e) => 
{
    txtblkShowStatus.Text = item; 
    if (((Int32)((RadioButton)o).Tag).Equals(2)) 
    {
        MessageBox.Show("hurrey");
    }
};

You are mistakenly trying to use radio , which is declared above. 您错误地尝试使用上面声明的radio Instead, use what the anonymous method actually receives , which is o , or the Sender . 而是使用匿名方法实际收到的内容 ,即oSender

Create an event for when the index is changed on your radio button group. 为单选按钮组上的索引更改创建事件。 After this create a separate function to deserialize your XML file. 之后,创建一个单独的函数来反序列化您的XML文件。 Pass the given integer obtained from your XML file to the index changed event and then display the UI element accordingly. 将从XML文件获得的给定整数传递给索引更改事件,然后相应地显示UI元素。 Be sure to compare the integer on the .selectedIndex property of your radio button. 确保比较单选按钮的.selectedIndex属性上的整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM