簡體   English   中英

如何檢查是否已選中任何單選按鈕

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM