The RadioButton
The RadioButton also derives from ToggleButton and uses the same IsChecked property and the same Checked, Unchecked, and Indeterminate events. Along with these, the RadioButton adds a single property named GroupName, which allows you to control how radio buttons are placed into groups.
Ordinarily, radio buttons are grouped by their container. That means if you place three RadioButton controls in a single StackPanel, they form a group from which you can select just one of the three. On the other hand, if you place a combination of radio buttons in two separate StackPanel controls, you have two independent groups on your hands.
The GroupName property allows you to override this behavior. You can use it to create more than one group in the same container or to create a single group that spans multiple containers. Either way, the trick is simple—just give all the radio buttons that belong together the same group name.
Consider this example:
<StackPanel>
<Border Margin="5" Padding="5" BorderBrush="Yellow" BorderThickness="1" CornerRadius="5"> <StackPanel> <RadioButton Content="Group 1"></RadioButton> <RadioButton Content="Group 1"></RadioButton> <RadioButton Content="Group 1"></RadioButton> <RadioButton GroupName="Group2" Content="Group 2"></RadioButton> </StackPanel> </Border>
<Border Margin="5" Padding="5" BorderBrush="Yellow" BorderThickness="1" CornerRadius="5"> <StackPanel> <RadioButton Content="Group 3"></RadioButton> <RadioButton Content="Group 3"></RadioButton> <RadioButton Content="Group 3"></RadioButton> <RadioButton GroupName="Group2" Content="Group 2"></RadioButton> </StackPanel> </Border> </StackPanel>
Here, there are two containers holding radio buttons, but three groups (see Figure 5-8). The final radio button at the bottom of each group box is part of a third group. In this example, it makes for a confusing design, but there may be some scenarios where you want to separate a specific radio button from the pack in a subtle way without causing it to lose its group membership.
- Figure 5-8. Grouping radio buttons
Post a comment