vendredi 1 avril 2016

XAML WPF CheckBox Validation

I have a list of CheckBox'es. I would like the user to select at least one before click the next button.

I would want the Button to remain Enabled, but use a TextBlock below the CheckBox to show the prompt to select at least one CheckBox.

How can I check that.

Code:

XAML

<CheckBox x:Name="CheckBox1"  Content="CheckBox1" />
<CheckBox x:Name="CheckBox2"  Content="CheckBox2" />
<CheckBox x:Name="CheckBox3"  Content="CheckBox3" />
<CheckBox x:Name="CheckBox4"  Content="CheckBox4" />

<Button x:Name="NextButton" Click="NextButton_Click"/>

Code Behind

private void NextButton_Click(object sender, RoutedEventArgs e) {
    if (CheckBox1.IsChecked ?? false) {
        // do something
    }
   // same for other checkBoxes
}




Aucun commentaire:

Enregistrer un commentaire