Condividi tramite


Procedura: rilevare selezioni di controlli RadioButton

Aggiornamento: novembre 2007

Quando vengono raggruppati i controlli RadioButton, i pulsanti si escludono reciprocamente. Un utente può selezionare un solo elemento alla volta all'interno di un gruppo di RadioButton. L'applicazione cancella un elemento selezionato a livello di codice quando l'utente seleziona un nuovo elemento. La selezione di un controllo RadioButton viene determinata dallo stato della proprietà IsChecked. È possibile raggruppare i controlli RadioButton posizionandoli all'interno di un padre o assegnando loro un nome di gruppo. Nell'esempio di codice riportato di seguito vengono eseguite entrambe le azioni. I controlli RadioButton sono gli elementi figlio di StackPanel e il nome di gruppo è ExpandDirectionProperty.

Quando viene selezionato un controllo RadioButton, viene generato l'evento Checked. Come illustrato nell'esempio di codice riportato di seguito, se l'applicazione deve eseguire un'azione quando viene modificata la selezione RadioButton, è possibile aggiungere un gestore eventi per gestire l'evento Checked.

Esempio

<StackPanel>
  <RadioButton Name="ExpandDown" Margin="0,10,0,10" 
            IsChecked="True"
            Checked="ChangeExpandDirection"
            GroupName="ExpandDirectionProperty">
     Expand Down
  </RadioButton>
  <RadioButton Name="ExpandUp" Margin="0,0,0,10"
            Checked="ChangeExpandDirection"
            GroupName="ExpandDirectionProperty">
     Expand Up
  </RadioButton>
  <RadioButton Name="ExpandLeft" Margin="0,0,0,10"
            Checked="ChangeExpandDirection"
            GroupName="ExpandDirectionProperty">
    Expand Left
  </RadioButton>
  <RadioButton Name="ExpandRight" Margin="0,0,0,10"
            Checked="ChangeExpandDirection"
            GroupName="ExpandDirectionProperty">
    Expand Right
  </RadioButton>
</StackPanel>
Private Sub ChangeExpandDirection(ByVal Sender As Object, ByVal e As RoutedEventArgs)
    If (ExpandDown.IsChecked) Then
        myExpander.ExpandDirection = ExpandDirection.Down
    ElseIf (ExpandUp.IsChecked) Then
        myExpander.ExpandDirection = ExpandDirection.Up
    ElseIf (ExpandLeft.IsChecked) Then
        myExpander.ExpandDirection = ExpandDirection.Left
    ElseIf (ExpandRight.IsChecked) Then
        myExpander.ExpandDirection = ExpandDirection.Right
    End If

    'Expand myExpander so it is easier to see the effect of changing 
    'the ExpandDirection property for My Expander
    myExpander.IsExpanded = True
End Sub
private void ChangeExpandDirection(object sender, RoutedEventArgs e)
{
    if ((Boolean)ExpandDown.IsChecked)
        myExpander.ExpandDirection = ExpandDirection.Down;
    else if ((Boolean)ExpandUp.IsChecked)
        myExpander.ExpandDirection = ExpandDirection.Up;
    else if ((Boolean)ExpandLeft.IsChecked)
        myExpander.ExpandDirection = ExpandDirection.Left;
    else if ((Boolean)ExpandRight.IsChecked)
        myExpander.ExpandDirection = ExpandDirection.Right;

    //Expand myExpander so it is easier to see the effect of changing 
    //the ExpandDirection property for My Expander
    myExpander.IsExpanded = true;
}