[英]How to check if any radiobutton is checked
我有4个单选按钮,我想检查是否已选中。
这是我的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
按钮后,我想显示一个消息对话框。 我该如何编码? 到目前为止,这是我的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();
}
}
您可以为StackPanel
指定一个名称,然后进行如下检查:
if (!(radioButtonStackPanel.Children.OfType<RadioButton>().Any(rb => rb.IsChecked == true)))
只需记住为StackPanel
指定一个名称即可,例如:
<StackPanel Background="#FF3A3A49" Grid.Column="1" Grid.Row="4" x:Name="radioButtonStackPanel">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.