如何检查是否检查了任何radiobutton

时间:2016-04-05 19:21:54

标签: c# wpf

我有4个radiobuttons,我想检查是否有人检查过。

这是我的WPF代码:

<StackPanel Background="#FF3A3A49" Grid.Column="1" Grid.Row="4">
    <RadioButton x:Name="rtnRight" GroupName="answer"  HorizontalAlignment="Center" VerticalAlignment="Top" Foreground="White" Content="value0" BorderBrush="White"/>
    <RadioButton Content="value1" GroupName="answer" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
    <RadioButton Content="value2" GroupName="answer" HorizontalAlignment="Center" VerticalAlignment="Bottom"  Foreground="White" />
    <RadioButton Content="value3" GroupName="answer" HorizontalAlignment="Center" VerticalAlignment="Bottom"  Foreground="White" />
</StackPanel>
<Button x:Name="btnNext" Grid.Column="1" Grid.Row="5" Content="Dalej" VerticalAlignment="Center" HorizontalAlignment="Center" Width="100" Height="50" Margin="0 0 0 0 " Foreground="#FFAC0303" BorderBrush="#FFC1C1C1" Background="#66FFFFFF" Click="btnNext_Click"></Button>

单击btnNext并且未选中任何单选按钮后,我想显示一个消息对话框。我该如何编码呢?到目前为止,这是我的btnNext_Click函数。

private async void btnNext_Click(object sender, RoutedEventArgs e)
    {    
        if ("any radiobutton checked?")
        {
           await new Windows.UI.Popups.MessageDialog("Choose at least one answer").ShowAsync();
        }
    }

1 个答案:

答案 0 :(得分:1)

您可以为StackPanel指定一个名称,然后点击:

if (!(radioButtonStackPanel.Children.OfType<RadioButton>().Any(rb => rb.IsChecked == true)))

请记住为StackPanel指定一个名称,如:

<StackPanel Background="#FF3A3A49" Grid.Column="1" Grid.Row="4" x:Name="radioButtonStackPanel">