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。请帮助我们解决这个疑问。使用上面的代码,组中单选按钮的最后一个索引被选中,但我们想要所选无线电的索引。提前谢谢。