简体   繁体   English

如何检查是否已选中任何单选按钮

[英]How to check if any radiobutton is checked

I have 4 radiobuttons and I want to check if any are checked. 我有4个单选按钮,我想检查是否已选中。

This is my WPF code: 这是我的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>

After I click btnNext and no radiobuttons have been checked, I want to show a message dialog. 单击btnNext并且未选中任何btnNext按钮后,我想显示一个消息对话框。 How can I code this? 我该如何编码? This is my btnNext_Click function so far. 到目前为止,这是我的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();
        }
    }

You can specify a name for your StackPanel and then check like: 您可以为StackPanel指定一个名称,然后进行如下检查:

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

Just remember to specify a name for StackPanel like: 只需记住为StackPanel指定一个名称即可,例如:

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

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

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