Toggle Buttons CheckBox and RadioButton

The ToggleButton provides the base functionality for both radio buttons and check boxes, which are controls that can switch states. Table 3-15 shows its key properties.

Table 3-15. Key Properties of the System.Windows.Controls.Primitives.ToggleButton Class Property Type Description

IsChecked Nullable<bool> Indicates true if checked, false if not, and null if in an indeterminate state. If IsThreeState is set to true, the user can cause this property's value to cycle between true/ false/null.

IsThreeState bool Gets/sets whether the control supports three states. If false, the button supports only two states.

The ToggleButton class introduces three new events: Checked, Unchecked, and Indeterminate. These events use RoutedEventArgs as the event argument type and capture the various states a ToggleButton can switch into. The two classes that inherit from ToggleButton are CheckBox and RadioButton. The main distinguishing factor between check boxes and radio buttons is that radio buttons can be grouped, so only one specific radio button within a group can be selected at any given moment. Table 3-16 describes the key properties of RadioButton. If no group is specified, all ungrouped radio buttons within a single parent control become part of the same group.

Table 3-16. Key Properties of the System.Windows.Controls.RadioButton Class

Property

Type

Description

GroupName

string

Gets/sets the name of the group to which this radio button belongs

Here's the XAML for the check boxes shown in Figure 3-11:

<CheckBox x:Name="checkBox" Canvas.Left="25" Canvas.Top="20" IsChecked="True" Content="Checked"/>

<CheckBox x:Name="checkBox2" Canvas.Left="25" Canvas.Top="40" IsChecked="False" Content="Unchecked"/> <CheckBox x:Name="checkBox3" Canvas.Left="25" Canvas.Top="60"

IsChecked="" IsThreeState="True" Content="Indeterminate"/>

The radio buttons are given unique names, but they share the group name to ensure the mutual exclusion functionality.

<RadioButton x:Name="radioButton1" GroupName="group1"

Canvas.Left="40" Canvas.Top="20" Content="Red"/> <RadioButton x:Name="radioButton2" GroupName="group1"

Canvas.Left="40" Canvas.Top="40" Content="Green"/> <RadioButton x:Name="radioButton3" GroupName="group1"

Canvas.Left="40" Canvas.Top="60" Content="Blue"/> <RadioButton x:Name="radioButton4" GroupName="group1"

Canvas.Left="40" Canvas.Top="80" Content="Cyan"/>

+6 -1

Average user rating: 4.4 stars out of 7 votes

Post a comment

  • Receive news updates via email from this site