简体   繁体   中英

Check if each radiobutton group has one radiobutton selected/checked

I am currently developing a WPF app. If the user clicks a button, the program should check if one radiobutton per group was checked. Here is an excerpt from XAML:

<Border Grid.Row="3" Grid.Column="1" BorderBrush="Black" BorderThickness="2" Margin="78,0,0,0" Background="LightGray">
            <WrapPanel x:Name="wrpnlElektro" Height="65" Margin="0,0,0,-12" Background="LightGray" Grid.Column="1" Grid.Row="2" Grid.RowSpan="2" VerticalAlignment="Top">
                <Label x:Name="lblElektro" Content="Elektro" FontWeight="Bold" Margin="20,15,0,15"/>
                <Label x:Name="lblElektroBauseitig" Content="bauseitig" Margin="50,15,0,15" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <RadioButton GroupName="Elektro" x:Name="rbElektroBauseitig" Margin="20,22,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Label x:Name="lblElektroLuxhaus" Content="Luxhaus" Margin="40,15,0,15" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <RadioButton GroupName="Elektro" x:Name="rbElektroLuxhaus" Margin="20,22,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            </WrapPanel>
        </Border>

        <Border Grid.Row="4" Grid.Column="1" BorderBrush="Black" BorderThickness="2" Margin="78,0,0,0" Background="LightGray">
            <WrapPanel x:Name="wrpnlSAT" Height="60" Margin="0,0,0,0" Background="LightGray" VerticalAlignment="Top">
                <Label x:Name="lblSAT" Content="SAT-Anlage" FontWeight="Bold" Margin="20,15,0,15"/>
                <Label x:Name="lblSATJa" Content="Ja" Margin="61,15,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <RadioButton GroupName="SAT" x:Name="rbSATJa" Margin="20,22,0,22"/>
                <Label x:Name="lblSATVorb" Content="Vorber." Margin="44,15,0,15" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <RadioButton GroupName="SAT" x:Name="rbSATVorb" Margin="20,22,0,22"/>
                <Label x:Name="lblSATNein" Content="Nein" Margin="40,15,0,15" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <RadioButton GroupName="SAT" x:Name="rbSATNein" Margin="20,22,0,22"/>
            </WrapPanel>
        </Border>

        <Border Grid.Row="5" Grid.Column="1" BorderBrush="Black" BorderThickness="2" Margin="78,0,0,0" Background="LightGray">
            <WrapPanel x:Name="wrpnlPV" Margin="0,0,0,-8" Background="LightGray">
                <Label x:Name="lblPV" Content="PV-Anlage" FontWeight="Bold" Margin="20,15,0,15"/>
                <Label x:Name="lblPVJa" Content="Ja" Margin="68,15,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <RadioButton GroupName="PV" x:Name="rbPVJa" Margin="20,22,0,22" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Label x:Name="lblPVVorb" Content="Vorber." Margin="44,15,0,15" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <RadioButton GroupName="PV" x:Name="rbPVVorb" Margin="20,22,0,22" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Label x:Name="lblPVNein" Content="Nein" Margin="40,15,0,15" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <RadioButton GroupName="PV" x:Name="rbPVNein" Margin="20,22,0,22" HorizontalAlignment="Left" VerticalAlignment="Top"/>
            </WrapPanel>
        </Border>

So basically I'm saying: "User, if you didn't make one selection for every "radiobutton-container" then a MessageBox appears with a warning.

How do I do this in either XAML or code behind? If possible, please give me a code example instead of an instructive comment. Thank you very much!

set IsChecked=true for the first RadioButton in each group by default. It cannot be unchecked from UI and then you will not need validation.

alternatively, check every group:

bool success = 
     (rbElektroBauseitig.IsChecked == true || rbElektroLuxhaus.IsChecked == true) &&
     (rbSATJa.IsChecked == true || rbSATVorb.IsChecked == true  || rbSATVorb.IsChecked == true) &&
     (rbPVJa.IsChecked == true || rbPVVorb.IsChecked == true  || rbPVNein.IsChecked == true);

if (!success) 
     MessageBox.Show("Dear User, you didn't make selection");

Also, you could use Linq

You would create a statement for each container. If the variable returned is null there is nothing selected. This will also return the RadioButton including it's name of the selected item.

    var ElektroGroup = wrpnlElektro.Children.OfType<RadioButton>().FirstOrDefault(x => x.IsChecked.Value);

    if(ElektroGroup == null || ...any other group == null...)
    {
        //TODO nothing found
        return;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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