ktos1234
ktos1234

Reputation: 207

How to check if any radiobutton is checked

I have 4 radiobuttons and I want to check if any are checked.

This is my WPF code:

<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. How can I code this? This is my btnNext_Click function so far.

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();
        }
    }

Upvotes: 0

Views: 1352

Answers (1)

Habib
Habib

Reputation: 223187

You can specify a name for your StackPanel and then check like:

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

Just remember to specify a name for StackPanel like:

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

Upvotes: 1

Related Questions