In This Article

ToggleSwitch

The ToggleSwitch is a control that is typically toggled between two states ("On" and "Off") and optionally supports a third state ("Indeterminate"").

Screenshot

The control is composed of a Knob element whose position indicates the state of the control, a Track which defines the bounds for the Knob, and one or more Content elements which display content associated with the current state.

Toggle States

Two states are supported by default: "On" and "Off". An optional "Indeterminate" state may also be enabled by setting the IsThreeState property to true.

The current state is defined by the IsChecked property. The type is a Nullable<Boolean> where the following defines which states are represented by each value:

Value Toggle State
false The switch state is "Off"".
true The switch state is "On"".
null The switch state is "Indeterminate"".

To change the current state, set the IsChecked property to the desired value. Handle the Checked event to respond to changes in state.

Note

The IsThreeState property only defines the behavior of the control. The value of IsChecked can be programmatically set to null even when IsThreeState is false.

Animation

The ToggleSwitch has several animations which are applied to elements of the control to improve the visualization. When the current state changes, the Knob element will move smoothly to the new position while transitioning the state-based System.Windows.Media.Brush properties for the Track and Knob. Changes in the Knob element size are also animated and can be observed when moving the pointer over the control.

Animations are automatically disabled, as appropriate, based on system settings. To manually turn off animations, set the IsAnimationEnabled property to false.

Important

The Track element's background and border properties will only support smooth animations when using System.Windows.Media.SolidColorBrush. Other brushes can be used, but the values will be applied immediately when state changes. The Knob element fully supports animated transitions for all brushes.

Knob Shadow

While not enabled by default, the ToggleSwitch uses ShadowChrome to support a drop shadow for the Knob element with the following important members:

Member Description
IsKnobShadowEnabled Property Gets or sets whether the shadow is enabled for the knob element. The default value is false.
KnobShadowElevation Property Gets or sets the elevation of the shadow for the knob element, a value from 0 to 24. Higher elevations render larger shadows. The default value is 0, which means the shadow will not be rendered.
KnobShadowDirection Property Gets or sets the direction (in degrees) of the shadow for the knob element where 270 is down, 315 is lower-right, etc. The default value is 270.
KnobShadowOpacity Property Gets or sets the opacity of the shadow for the knob element as a value from 0.0 to 1.0. The default value is 0.3.

The following example demonstrates adding a shadow:

xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
...
<shared:ToggleSwitch IsKnobShadowEnabled="True" KnobShadowElevation="2" />

Customize Layout

Several options are available to customize the layout of the control.

Orientation

The Orientation can be set to Horizontal (default) or Vertical.

When set to Horizontal, the switch moves from left ("Off") to right ("On"). Labels for current state are commonly displayed to the left and/or right of the switch, based on ContentPlacement.

When set to Vertical, the switch moves from bottom ("Off") to top ("On"). Labels for current state are commonly displayed above and/or below the switch, based on ContentPlacement.

Content Placement

The current state of the switch is typically complemented by a label on one side whose value corresponds to the state. Use the ContentPlacement property to control the position of the content or hide it completely. The following values are available:

Content Placement Description
None No content will be displayed.
Near Content is only positioned on the left (horizontal) or top (vertical), based on orientation and current state.
Far Content is only positioned on the right (horizontal) or bottom (vertical), based on orientation and current state.
NearAndFar The "Off" and "On" content is always positioned on the left and right (horizontal) or bottom and top (vertical), based on orientation. No content is displayed for "Indeterminate"".
Track Content is positioned within the Track portion of the control. The "Off" and "On" content is aligned to be positioned opposite of the Knob element. The "Indeterminate" content is centered behind the Knob element is not recommended for use. See important note below regarding use of ToggleSwitchContentPlacement.Track.
Important

When using ToggleSwitchContentPlacement.Track, it may be necessary to customize the FontSize, Foreground, OffContent, OnContent, and/or TrackContentMargin properties to ensure the content is properly displayed since the default style is intended for use with Near and/or Far content placement.

Customize Appearance

Several options are available to customize the appearance of the control.

Understanding Extent and Ascent

Instead of Width and Height, the ToggleSwitch control uses Extent- and Ascent-related properties which work for any Orientation. The Extent-related properties are associated with the primary axis of the control (e.g., x-axis for Horizontal orientation, and y-axis for Vertical orientation). The Ascent-related properties are associated with the axis which is perpendicular to the primary axis (e.g., y-axis for Horizontal orientation, and x-axis for Vertical orientation).

For Horizontal orientation, the primary axis is the x-axis (left-to-right):

  • Extent = Width
  • Ascent = Height

For Vertical orientation, the primary axis is the y-axis (bottom-to-top):

  • Extent = Height
  • Ascent = Width

By using these Extent- and Ascent-related properties instead of Width and Height, values can be assigned which will be appropriate for the control at either orientation.

Track and Knob Size

Use the TrackExtent and TrackAscent properties to define the size of the Track element.

Use the KnobExtent and KnobAscent properties to define the size of the Knob element.

Warning

When customizing the size of the Knob element, it may also be necessary to set the values for the MaxKnobExtent and MaxKnobAscent properties. These values are used to properly reserve space for the Knob element in the event the size changes in response to user interaction (e.g., moving the pointer over the control).

Note that even if your configuration does not increase the size of the Knob element during interaction, the default Style of the control defines values for MaxKnobExtent and MaxKnobAscent which are appropriate for the default Style. If you are using the default Style (or one that is based on it) you may still need to define the maximum values, especially if you are making the Knob smaller than the default size.

The following example for a horizontal ToggleSwitch demonstrates how to set the Knob element to a custom 30x30 size which grows to 32x32 on mouse over and then 34x32 when pressed:

xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
...
<shared:ToggleSwitch Orientation="Horizontal">
	<shared:ToggleSwitch.Style>
		<Style TargetType="shared:ToggleSwitch">
			<Setter Property="KnobExtent" Value="30" /><!-- Width (for Horizontal) -->
			<Setter Property="KnobAscent" Value="30" /><!-- Height (for Horizontal) -->
			<Setter Property="MaxKnobExtent" Value="34" />
			<Setter Property="MaxKnobAcent" Value="32" />
			<Style.Triggers>
				<MultiTrigger>
					<MultiTrigger.Conditions>
						<Condition Property="IsMouseOver" Value="True" />
						<Condition Property="IsPressed" Value="False" />
					</MultiTrigger.Conditions>
					<Setter Property="KnobExtent" Value="32" /><!-- Width (for Horizontal) -->
					<Setter Property="KnobAscent" Value="32" /><!-- Height (for Horizontal) -->
				</MultiTrigger>
				<Trigger Property="IsPressed" Value="True">
					<Setter Property="KnobExtent" Value="34" /><!-- Width (for Horizontal) -->
					<Setter Property="KnobAscent" Value="32" /><!-- Height (for Horizontal) -->
				</Trigger>
			</Style.Triggers>
		</Style>
	</shared:ToggleSwitch.Style>
</shared:ToggleSwitch>

Brushes

The Knob and Track elements have several System.Windows.Media.Brush properties available to customize the appearance of each element. These properties are defined with explicit values corresponding to "Off", "On", and "Indeterminate" state to simplify state-specific appearance without the need for defining an explicit Style.

For example, use the OffTrackBackground and OffKnobBackground properties to customize the Track and Knob elements when in the "Off" state.

Customize Content

The OffContent, OnContent, and IndeterminateContent properties are available to customize the content displayed for each state.

By default, each content property is initialized to a localizable String resource value. The following resources are configured:

Resource key Description
UIToggleSwitchOffText The text displayed for OffContent. The default value is "Off".
UIToggleSwitchOnText The text displayed for OnContent. The default value is "On".
UIToggleSwitchIndeterminateText The text displayed for IndeterminateContent. The default value is "-".

This code shows how to set custom values for string resources.

ActiproSoftware.Products.Shared.SR.SetCustomString(ActiproSoftware.Products.Shared.SRName.UIToggleSwitchOffText.ToString(), "No");
ActiproSoftware.Products.Shared.SR.SetCustomString(ActiproSoftware.Products.Shared.SRName.UIToggleSwitchOnText.ToString(), "Yes");

See the Customizing String Resources topic for additional details.

Tip

For more advanced layouts, any Object which is supported by ContentPresenter can be assigned as content.

Important

While ToggleSwitch derives from System.Windows.Controls.ContentControl, it does not support direct content using the Content property.

Theme Assets

See the Theme Reusable Assets topic for more details on using and customizing theme assets. The following reusable assets are used by ToggleSwitch:

Asset Resource Key Description
ContainerForegroundLowerDisabledBrushKey Assigned to the following properties when the control is disabled: Foreground, IndeterminateTrackBackground, IndeterminateTrackBorderBrush, OffKnobBackground, OffKnobBorderBrush, OffTrackBorderBrush, OnTrackBackground, and OnTrackBorderBrush.
ToggleSwitchKnobOffBrushKey Assigned to the following properties when in the "Off" state: OffKnobBackground, OffKnobBorderBrush, and OffTrackBorderBrush.
ToggleSwitchKnobOnBrushKey Assigned to the following properties when in the "On" or "Indeterminate" states: OnKnobBackground, OnKnobBorderBrush, IndeterminateKnobBackground, and IndeterminateKnobBorderBrush.
ToggleSwitchTrackOffNormalBrushKey Assigned to the following properties when in the "Off" state: OffTrackBackground.
ToggleSwitchTrackOffHoverBrushKey Assigned to the following properties when in the "Off" state and the mouse is over the control: OffTrackBackground.
ToggleSwitchTrackOffPressedBrushKey Assigned to the following properties when in the "Off" state and the switch is pressed: OffTrackBackground.
ToggleSwitchTrackOnNormalBrushKey Assigned to the following properties when in the "On" state: OnTrackBackground.
ToggleSwitchTrackOnHoverBrushKey Assigned to the following properties when in the "On" state and the mouse is over the control: OnTrackBackground.
ToggleSwitchTrackOnPressedBrushKey Assigned to the following properties when in the "On" state and the switch is pressed: OnTrackBackground.
ToggleSwitchTrackIndeterminateNormalBrushKey Assigned to the following properties when in the "Indeterminate" state: IndeterminateTrackBackground.
ToggleSwitchTrackIndeterminateHoverBrushKey Assigned to the following properties when in the "Indeterminate" state and the mouse is over the control: IndeterminateTrackBackground.
ToggleSwitchTrackIndeterminatePressedBrushKey Assigned to the following properties when in the "Indeterminate" state and the switch is pressed: IndeterminateTrackBackground.