0

该 xaml 包含:

<RadioButton Content="{Binding Content}" Height="70" Width="400" HorizontalAlignment="Left" Margin="0,0,0,0" Name="rbopt" VerticalAlignment="Top" IsChecked="False" FontSize="20"  Tag="{Binding Correct}" Click="radioButton1_Click_1" IsHitTestVisible="True" />

我们在文本块中显示所选单选按钮的 id 为:

<TextBlock Grid.Row="3" Height="225" HorizontalAlignment="Left" Margin="71,28,0,0" Name="txtoptid"   VerticalAlignment="Top" Width="257" />
//table name is Options

private void radioButton1_Click_1(object sender, RoutedEventArgs e)

    {
        RadioButton radio = sender as RadioButton;

        //convert string to long

        string id = txtquesID.Text.ToString();
        long qid;
        long.TryParse(id, out qid);
        //getting count of number of options
        var opp = (from Option opt in catAppDB.Options where opt.Q_id==qid
                  select opt).Count();

         //getting the option records
        var opp1 = from Option opt in catAppDB.Options
                   where opt.Q_id == qid
                   select opt;

       var opt1list = opp1.ToList();

       for (int i = 0; i < opp; i++)
       {
           //following loop gets executed opp times because for any option selected in the group,radio.IsChecked is always true hence it displays the id of the last option in the radio button group,it should get executed only once.
           if (radio.IsChecked == true)    
           {
               txtoptid.Text = "";
               var opt2 = opt1list[i] as Option;
               var opt3 = opt2._id;
               txtoptid.Text = opt3.ToString();
           }
       } }

我正在尝试选择用户单击的特定单选按钮的索引,但对于我们单击的任何单选按钮,radio.IsChecked 变为 true。请帮助我们解决这个疑问。使用上面的代码,组中单选按钮的最后一个索引被选中,但我们想要所选单选的索引。提前致谢。

4

0 回答 0