Freigeben über


How to: Detect RadioButton Selections

When RadioButton controls are grouped, the buttons are mutually exclusive. A user can select only one item at a time within a RadioButton group. The application programmatically clears a selected item when the user selects a new item. Whether a RadioButton is selected is determined by the state of its IsChecked property. You can group RadioButton controls by placing them inside a parent or by giving them a group name. The following code example does both; the RadioButton controls are the child elements of a StackPanel and the group name is ExpandDirectionProperty.

When a RadioButton is selected the Checked event fires. As the following code example shows if your application needs to take some action when the RadioButton selection changes you can add an event handler to handle the Checked event.

Example

<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>

See Also

Concepts

RadioButton Overview