In This Article

ToggleSwitch Class

Represents a switch that allows users to toggle the state of an item.

[TemplatePart(Name = "PART_SwitchKnob", Type = typeof(Grid))]
[TemplatePart(Name = "PART_SwitchKnobIndeterminate", Type = typeof(ElementChrome))]
[TemplatePart(Name = "PART_SwitchKnobOff", Type = typeof(ElementChrome))]
[TemplatePart(Name = "PART_SwitchKnobOn", Type = typeof(ElementChrome))]
[TemplatePart(Name = "PART_SwitchKnobShadow", Type = typeof(ShadowChrome))]
[TemplatePart(Name = "PART_KnobTranslateTransform", Type = typeof(TranslateTransform))]
[TemplatePart(Name = "PART_SwitchTrack", Type = typeof(FrameworkElement))]
[TemplatePart(Name = "PART_Thumb", Type = typeof(Thumb))]
public class ToggleSwitch : ToggleButton, IOrientedElement
Inheritance:
object Visual UIElement FrameworkElement Control ContentControl ButtonBase ToggleButton object
Implements:
IOrientedElement

Constructors

ToggleSwitch()

Initializes an instance of the class.

public ToggleSwitch()

Properties

ContentPlacement

The placement of OnContent, OffContent, and IndeterminateContent.

public ToggleSwitchContentPlacement ContentPlacement { get; set; }

Property Value

ToggleSwitchContentPlacement:

The default value is Near.

FarContentMargin

The margin applied to content displayed on the far side of the switch.

public Thickness FarContentMargin { get; set; }

Property Value

Thickness

IndeterminateContent

The content for when the switch is "indeterminate".

public object? IndeterminateContent { get; set; }

Property Value

object

IndeterminateContentTemplate

The template used to display the IndeterminateContent.

public DataTemplate? IndeterminateContentTemplate { get; set; }

Property Value

DataTemplate

IndeterminateContentTemplateSelector

public DataTemplateSelector? IndeterminateContentTemplateSelector { get; set; }

Property Value

DataTemplateSelector

IndeterminateKnobBackground

The brush used for the background of the knob element when the switch is "indeterminate".

public Brush? IndeterminateKnobBackground { get; set; }

Property Value

Brush

IndeterminateKnobBorderBrush

The brush used for the border of the knob element when the switch is "indeterminate".

public Brush? IndeterminateKnobBorderBrush { get; set; }

Property Value

Brush

IndeterminateTrackBackground

The brush used for the background of the track element when the switch is "indeterminate".

public Brush? IndeterminateTrackBackground { get; set; }

Property Value

Brush

IndeterminateTrackBorderBrush

The brush used for the border of the track element when the switch is "indeterminate".

public Brush? IndeterminateTrackBorderBrush { get; set; }

Property Value

Brush

IsAnimationEnabled

Indicates whether animations are enabled for an element, when supported.

public bool IsAnimationEnabled { get; set; }

Property Value

bool:

true if animations are enabled; otherwise, false. The default value is true.

IsKnobShadowEnabled

Indicates whether the shadow is enabled for the knob element.

public bool IsKnobShadowEnabled { get; set; }

Property Value

bool:

true if the shadow is enabled; otherwise, false. The default value is false.

Remarks

Set this property to false to prevent the shadow from rendering.

KnobAscent

The size of the knob element on the perpendicular axis (i.e., height when horizontal, width when vertical).

public double KnobAscent { get; set; }

Property Value

double:

The default value is 12.

Remarks

The largest possible value assigned to this property should also be assigned to MaxKnobAscent.

See Also

KnobBorderThickness

The thickness of the knob element border.

public Thickness KnobBorderThickness { get; set; }

Property Value

Thickness

KnobCornerRadius

The corner radius of the knob element.

public CornerRadius KnobCornerRadius { get; set; }

Property Value

CornerRadius

KnobExtent

The size of the knob element on the primary axis (i.e., width when horizontal, height when vertical).

public double KnobExtent { get; set; }

Property Value

double:

The default value is 12.

Remarks

The largest possible value assigned to this property should also be assigned to MaxKnobExtent.

See Also

KnobMargin

The margin applied to knob element.

public Thickness KnobMargin { get; set; }

Property Value

Thickness

KnobOpacity

The opacity of the knob element.

public double KnobOpacity { get; set; }

Property Value

double:

The default value is 1.

KnobShadowDirection

The direction of the shadow for the knob element, in degrees, where 270 is down, 315 is lower-right, etc.

public double KnobShadowDirection { get; set; }

Property Value

double:

The default value is 270, meaning down.

KnobShadowElevation

The elevation of the shadow for the knob element, a value from 0 to 24.

public int KnobShadowElevation { get; set; }

Property Value

int

Remarks

Higher elevations render larger shadows. 0 doesn't render a shadow. The IsKnobShadowEnabled property must be set to true for the shadow to render.

KnobShadowOpacity

The opacity of the shadow for the knob element.

public double KnobShadowOpacity { get; set; }

Property Value

double:

The default value is 0.3.

MaxKnobAscent

The maximum value which will be assigned to the KnobAscent property.

public double MaxKnobAscent { get; set; }

Property Value

double

Remarks

Assigning a value to this property is only necessary if the value of KnobAscent increases through user interaction (e.g., the ascent increases on mouse over) so that space is properly reserved in the layout to accommodate the maximum size.

See Also

MaxKnobExtent

The maximum value which will be assigned to the KnobExtent property.

public double MaxKnobExtent { get; set; }

Property Value

double

Remarks

Assigning a value to this property is only necessary if the value of KnobExtent increases through user interaction (e.g., the extent increases on mouse over) so that space is properly reserved in the layout to accommodate the maximum size.

See Also

NearContentMargin

The margin applied to content displayed on the near side of the switch.

public Thickness NearContentMargin { get; set; }

Property Value

Thickness

OffContent

The content for when the switch is "off".

public object? OffContent { get; set; }

Property Value

object

OffContentTemplate

The template used to display the OffContent

public DataTemplate? OffContentTemplate { get; set; }

Property Value

DataTemplate

OffContentTemplateSelector

The DataTemplateSelector to use for the OffContent.

public DataTemplateSelector? OffContentTemplateSelector { get; set; }

Property Value

DataTemplateSelector

OffKnobBackground

The brush used for the background of the knob element when the switch is "off".

public Brush? OffKnobBackground { get; set; }

Property Value

Brush

OffKnobBorderBrush

The brush used for the border of the knob element when the switch is "off".

public Brush? OffKnobBorderBrush { get; set; }

Property Value

Brush

OffTrackBackground

The brush used for the background of the track element when the switch is "off".

public Brush? OffTrackBackground { get; set; }

Property Value

Brush

OffTrackBorderBrush

The brush used for the border of the track element when the switch is "off".

public Brush? OffTrackBorderBrush { get; set; }

Property Value

Brush

OnContent

The content for when the switch is "on".

public object? OnContent { get; set; }

Property Value

object

OnContentTemplate

The template used to display the OnContent.

public DataTemplate? OnContentTemplate { get; set; }

Property Value

DataTemplate

OnContentTemplateSelector

The DataTemplateSelector to use for the OnContent.

public DataTemplateSelector? OnContentTemplateSelector { get; set; }

Property Value

DataTemplateSelector

OnKnobBackground

The brush used for the background of the knob element when the switch is "on".

public Brush? OnKnobBackground { get; set; }

Property Value

Brush

OnKnobBorderBrush

The brush used for the border of the knob element when the switch is "on".

public Brush? OnKnobBorderBrush { get; set; }

Property Value

Brush

OnTrackBackground

The brush used for the background of the track element when the switch is "on".

public Brush? OnTrackBackground { get; set; }

Property Value

Brush

OnTrackBorderBrush

The brush used for the border of the track element when the switch is "on".

public Brush? OnTrackBorderBrush { get; set; }

Property Value

Brush

Orientation

The orientation of the control.

public Orientation Orientation { get; set; }

Property Value

Orientation:

The default value is Horizontal.

TrackAscent

The size of the track element on the perpendicular axis (i.e., height when horizontal, width when vertical).

public double TrackAscent { get; set; }

Property Value

double:

The default value is 20.

TrackBorderThickness

The thickness of the track element border.

public Thickness TrackBorderThickness { get; set; }

Property Value

Thickness

TrackContentMargin

The margin applied to content displayed on the track.

public Thickness TrackContentMargin { get; set; }

Property Value

Thickness

TrackCornerRadius

The corner radius of the track element.

public CornerRadius TrackCornerRadius { get; set; }

Property Value

CornerRadius

TrackExtent

The size of the track element on the primary axis (i.e., width when horizontal, height when vertical).

public double TrackExtent { get; set; }

Property Value

double:

The default value is 40.

TrackOpacity

The opacity of the track element as a value from 0 to 1.

public double TrackOpacity { get; set; }

Property Value

double:

The default value is 1.

Methods

OnApplyTemplate()

When overridden in a derived class, is invoked whenever application code or internal processes call ApplyTemplate().

public override void OnApplyTemplate()

OnPropertyChanged(DependencyPropertyChangedEventArgs?)

Invoked whenever the effective value of any dependency property on this FrameworkElement has been updated. The specific dependency property that changed is reported in the arguments parameter. Overrides System.Windows.DependencyObject.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs).

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs? e)
Parameter Type Description
e DependencyPropertyChangedEventArgs

The event data that describes the property that changed, as well as old and new values.

Fields

ContentPlacementProperty

Defines the ContentPlacement property.

public static readonly DependencyProperty ContentPlacementProperty

FarContentMarginProperty

Defines the FarContentMargin property.

public static readonly DependencyProperty FarContentMarginProperty

IndeterminateContentProperty

Defines the IndeterminateContent property.

public static readonly DependencyProperty IndeterminateContentProperty

IndeterminateContentTemplateProperty

Defines the IndeterminateContentTemplate property.

public static readonly DependencyProperty IndeterminateContentTemplateProperty

IndeterminateContentTemplateSelectorProperty

public static readonly DependencyProperty IndeterminateContentTemplateSelectorProperty

IndeterminateKnobBackgroundProperty

Defines the IndeterminateKnobBackground property.

public static readonly DependencyProperty IndeterminateKnobBackgroundProperty

IndeterminateKnobBorderBrushProperty

Defines the IndeterminateKnobBorderBrush property.

public static readonly DependencyProperty IndeterminateKnobBorderBrushProperty

IndeterminateTrackBackgroundProperty

Defines the IndeterminateTrackBackground property.

public static readonly DependencyProperty IndeterminateTrackBackgroundProperty

IndeterminateTrackBorderBrushProperty

Defines the IndeterminateTrackBorderBrush property.

public static readonly DependencyProperty IndeterminateTrackBorderBrushProperty

IsAnimationEnabledProperty

Defines the IsAnimationEnabled property.

public static readonly DependencyProperty IsAnimationEnabledProperty

IsKnobShadowEnabledProperty

Defines the IsKnobShadowEnabled property.

public static readonly DependencyProperty IsKnobShadowEnabledProperty

KnobAscentProperty

Defines the KnobAscent property.

public static readonly DependencyProperty KnobAscentProperty

KnobBorderThicknessProperty

Defines the KnobBorderThickness property.

public static readonly DependencyProperty KnobBorderThicknessProperty

KnobCornerRadiusProperty

Defines the KnobCornerRadius property.

public static readonly DependencyProperty KnobCornerRadiusProperty

KnobExtentProperty

Defines the KnobExtent property.

public static readonly DependencyProperty KnobExtentProperty

KnobMarginProperty

Defines the KnobMargin property.

public static readonly DependencyProperty KnobMarginProperty

KnobOpacityProperty

Defines the KnobOpacity property.

public static readonly DependencyProperty KnobOpacityProperty

KnobShadowDirectionProperty

Defines the KnobShadowDirection property.

public static readonly DependencyProperty KnobShadowDirectionProperty

KnobShadowElevationProperty

Defines the KnobShadowElevation property.

public static readonly DependencyProperty KnobShadowElevationProperty

KnobShadowOpacityProperty

Defines the KnobShadowOpacity property.

public static readonly DependencyProperty KnobShadowOpacityProperty

MaxKnobAscentProperty

Defines the MaxKnobAscent property.

public static readonly DependencyProperty MaxKnobAscentProperty

MaxKnobExtentProperty

Defines the MaxKnobExtent property.

public static readonly DependencyProperty MaxKnobExtentProperty

NearContentMarginProperty

Defines the NearContentMargin property.

public static readonly DependencyProperty NearContentMarginProperty

OffContentProperty

Defines the OffContent property.

public static readonly DependencyProperty OffContentProperty

OffContentTemplateProperty

Defines the OffContentTemplate property.

public static readonly DependencyProperty OffContentTemplateProperty

OffContentTemplateSelectorProperty

Defines the OffContentTemplateSelector property.

public static readonly DependencyProperty OffContentTemplateSelectorProperty

OffKnobBackgroundProperty

Defines the OffKnobBackground property.

public static readonly DependencyProperty OffKnobBackgroundProperty

OffKnobBorderBrushProperty

Defines the OffKnobBorderBrush property.

public static readonly DependencyProperty OffKnobBorderBrushProperty

OffTrackBackgroundProperty

Defines the OffTrackBackground property.

public static readonly DependencyProperty OffTrackBackgroundProperty

OffTrackBorderBrushProperty

Defines the OffTrackBorderBrush property.

public static readonly DependencyProperty OffTrackBorderBrushProperty

OnContentProperty

Defines the OnContent property.

public static readonly DependencyProperty OnContentProperty

OnContentTemplateProperty

Defines the OnContentTemplate property.

public static readonly DependencyProperty OnContentTemplateProperty

OnContentTemplateSelectorProperty

Defines the OnContentTemplateSelector property.

public static readonly DependencyProperty OnContentTemplateSelectorProperty

OnKnobBackgroundProperty

Defines the OnKnobBackground property.

public static readonly DependencyProperty OnKnobBackgroundProperty

OnKnobBorderBrushProperty

Defines the OnKnobBorderBrush property.

public static readonly DependencyProperty OnKnobBorderBrushProperty

OnTrackBackgroundProperty

Defines the OnTrackBackground property.

public static readonly DependencyProperty OnTrackBackgroundProperty

OnTrackBorderBrushProperty

Defines the OnTrackBorderBrush property.

public static readonly DependencyProperty OnTrackBorderBrushProperty

OrientationProperty

Defines the Orientation property.

public static readonly DependencyProperty OrientationProperty

TrackAscentProperty

Defines the TrackAscent property.

public static readonly DependencyProperty TrackAscentProperty

TrackBorderThicknessProperty

Defines the TrackBorderThickness property.

public static readonly DependencyProperty TrackBorderThicknessProperty

TrackContentMarginProperty

Defines the TrackContentMargin property.

public static readonly DependencyProperty TrackContentMarginProperty

TrackCornerRadiusProperty

Defines the TrackCornerRadius property.

public static readonly DependencyProperty TrackCornerRadiusProperty

TrackExtentProperty

Defines the TrackExtent property.

public static readonly DependencyProperty TrackExtentProperty

TrackOpacityProperty

Defines the TrackOpacity property.

public static readonly DependencyProperty TrackOpacityProperty

Extension Methods