ToggleSwitch ToggleSwitch ToggleSwitch Class

Definition

Represents a switch that can be toggled between two states.

public sealed class ToggleSwitchpublic sealed class ToggleSwitchPublic NotInheritable Class ToggleSwitch
<ToggleSwitch .../>
-or-
<ToggleSwitch ...>
  oneOrMorePropertyElementComponents
</ToggleSwitch>
Inheritance
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Inherited Members

Inherited properties

Inherited events

Inherited methods

Remarks

ToggleSwitch is a control that can be toggled between 2 states.

Toggle switch control

Use a ToggleSwitch control to let the user switch an option between on and off states. Use the IsOn property to determine the state of the switch. Handle the Toggled event to respond to changes in the state.

Control style and template

You can modify the default Style and ControlTemplate to give the control a unique appearance. For information about modifying a control's style and template, see Styling controls. The default style, template, and resources that define the look of the control are included in the generic.xaml file. For design purposes, generic.xaml is available in the (Program Files)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP&lt;SDK version>\Generic folder from a Windows Software Development Kit (SDK) installation. Styles and resources from different versions of the SDK might have different values.

Starting in Windows 10, version 1607 (Windows Software Development Kit (SDK) version 10.0.14393.0), generic.xaml includes resources that you can use to modify the colors of a control in different visual states without modifying the control template. In apps that target this software development kit (SDK) or later, modifying these resources is preferred to setting properties such as Background and Foreground. For more info, see the Light-weight styling section of the Styling controls article.

This table shows the resources used by the ToggleSwitch control.

Resource keyDescription
ToggleSwitchContainerBackgroundBackground color of entire control bounds at rest
ToggleSwitchContainerBackgroundPointerOverBackground color on hover
ToggleSwitchContainerBackgroundPressedBackground color when pressed
ToggleSwitchContainerBackgroundDisabledBackground color when disabled
ToggleSwitchContentForegroundLabel text color at rest
ToggleSwitchContentForegroundDisabledLabel text color when disabled
ToggleSwitchHeaderForegroundHeader text color at rest
ToggleSwitchHeaderForegroundDisabledHeader text color when disabled
ToggleSwitchFillOffBackground color of switch control part in "Off" state
ToggleSwitchFillOffPointerOverBackground olor of switch control part in "Off" state on hover
ToggleSwitchFillOffPressedBackground color of switch control part in "Off" state when pressed
ToggleSwitchFillOffDisabledBackground color of switch control part in "Off" state when disabled
ToggleSwitchFillOnBackground color of switch control part in "On" state
ToggleSwitchFillOnPointerOverBackground color of switch control part in "On" state on hover
ToggleSwitchFillOnPressedBackground color of switch control part in "On" state when pressed
ToggleSwitchFillOnDisabledBackground color of switch control part in "On" state when disabled
ToggleSwitchStrokeOffBorder color of switch control part in "Off" state
ToggleSwitchStrokeOffPointerOverBorder color of switch control part in "Off" state on hover
ToggleSwitchStrokeOffPressedBorder color of switch control part in "Off" state when pressed
ToggleSwitchStrokeOffDisabledBorder color of switch control part in "Off" state when disabled
ToggleSwitchStrokeOnBorder color of switch control part in "On" state
ToggleSwitchStrokeOnPointerOverBorder color of switch control part in "On" state on hover
ToggleSwitchStrokeOnPressedBorder color of switch control part in "On" state when pressed
ToggleSwitchStrokeOnDisabledBorder color of switch control part in "On" state when disabled
ToggleSwitchKnobFillOffColor of switch control's knob in "Off" state
ToggleSwitchKnobFillOffPointerOverColor of switch control's knob in "Off" state on hover
ToggleSwitchKnobFillOffPressedColor of switch control's knob in "Off" state when pressed
ToggleSwitchKnobFillOffDisabledColor of switch control's knob in "Off" state when disabled
ToggleSwitchKnobFillOnColor of switch control's knob in "On" state
ToggleSwitchKnobFillOnPointerOverColor of switch control's knob in "On" state on hover
ToggleSwitchKnobFillOnPressedColor of switch control's knob in "On" state when pressed
ToggleSwitchKnobFillOnDisabledColor of switch control's knob in "On" state when disabled

Examples

This example shows how to set the Header, OnContent, and OffContent properties of a toggle switch. The Toggled event is handled to turn a ProgressRing control on or off.

<StackPanel Orientation="Horizontal">
    <ToggleSwitch Header="Toggle Switch Example" 
        OffContent="Do work" OnContent="Working" 
        Toggled="ToggleSwitch_Toggled"/>  
    <ProgressRing x:Name="progress1"/>
</StackPanel>
        private void ToggleSwitch_Toggled(object sender, RoutedEventArgs e)
        {
            ToggleSwitch toggleSwitch = sender as ToggleSwitch;
            if (toggleSwitch != null)
            {
                if (toggleSwitch.IsOn == true)
                {
                    progress1.IsActive = true;
                    progress1.Visibility = Visibility.Visible;
                }
                else
                {
                    progress1.IsActive = false;
                    progress1.Visibility = Visibility.Collapsed;
                }
            }
        }

Constructors

ToggleSwitch() ToggleSwitch() ToggleSwitch() ToggleSwitch()

Initializes a new instance of the ToggleSwitch class.

public ToggleSwitch()public New()Public Sub New()public ToggleSwitch()
Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Properties

Header Header Header Header

Gets or sets the header content.

public object Header { get; set; }public object Header { get; set; }Public ReadWrite Property Header As objectpublic object Header { get; set; }
<ToggleSwitch Header="textContent" />
Value
object object object

The header content for the ToggleSwitch.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

HeaderProperty HeaderProperty HeaderProperty HeaderProperty

Identifies the Header dependency property.

public static DependencyProperty HeaderProperty { get; }public static DependencyProperty HeaderProperty { get; }Public Static ReadOnly Property HeaderProperty As DependencyPropertypublic static DependencyProperty HeaderProperty { get; }
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the Header dependency property.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

HeaderTemplate HeaderTemplate HeaderTemplate HeaderTemplate

Gets or sets the DataTemplate used to display the control's header.

public DataTemplate HeaderTemplate { get; set; }public DataTemplate HeaderTemplate { get; set; }Public ReadWrite Property HeaderTemplate As DataTemplatepublic DataTemplate HeaderTemplate { get; set; }
<ToggleSwitch>
  <ToggleSwitch.HeaderTemplate>
    singleDataTemplate
  </ToggleSwitch.HeaderTemplate>
</ToggleSwitch>
Value
DataTemplate DataTemplate DataTemplate

The DataTemplate used to display the control's header.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

HeaderTemplateProperty HeaderTemplateProperty HeaderTemplateProperty HeaderTemplateProperty

Identifies the HeaderTemplate dependency property.

public static DependencyProperty HeaderTemplateProperty { get; }public static DependencyProperty HeaderTemplateProperty { get; }Public Static ReadOnly Property HeaderTemplateProperty As DependencyPropertypublic static DependencyProperty HeaderTemplateProperty { get; }
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the HeaderTemplate dependency property.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

IsOn IsOn IsOn IsOn

Gets or sets a value that declares whether the state of the ToggleSwitch is "On".

public bool IsOn { get; set; }public bool IsOn { get; set; }Public ReadWrite Property IsOn As boolpublic bool IsOn { get; set; }
<ToggleSwitch IsOn="bool" />
Value
bool bool bool

true if the state is "On"; false if the state is "Off".

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

"On" state uses the template from OnContentTemplate. "Off" state uses the template from OffContentTemplate.

IsOnProperty IsOnProperty IsOnProperty IsOnProperty

Identifies the IsOn dependency property.

public static DependencyProperty IsOnProperty { get; }public static DependencyProperty IsOnProperty { get; }Public Static ReadOnly Property IsOnProperty As DependencyPropertypublic static DependencyProperty IsOnProperty { get; }
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the IsOn dependency property.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

OffContent OffContent OffContent OffContent

Provides the object content that should be displayed using the OffContentTemplate when this ToggleSwitch has state of "Off".

public object OffContent { get; set; }public object OffContent { get; set; }Public ReadWrite Property OffContent As objectpublic object OffContent { get; set; }
<ToggleSwitch OffContent="stringContent"/>
Value
object object object

The object content. In some cases this is a string, in other cases it is a single element that provides a root for further composition content. Probably the most common "set" usage is to place a binding here.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

OffContentProperty OffContentProperty OffContentProperty OffContentProperty

Identifies the OffContent dependency property.

public static DependencyProperty OffContentProperty { get; }public static DependencyProperty OffContentProperty { get; }Public Static ReadOnly Property OffContentProperty As DependencyPropertypublic static DependencyProperty OffContentProperty { get; }
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the OffContent dependency property.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

OffContentTemplate OffContentTemplate OffContentTemplate OffContentTemplate

Gets or sets the DataTemplate used to display the control's content while in "Off" state.

public DataTemplate OffContentTemplate { get; set; }public DataTemplate OffContentTemplate { get; set; }Public ReadWrite Property OffContentTemplate As DataTemplatepublic DataTemplate OffContentTemplate { get; set; }
<ToggleSwitch>
  <ToggleSwitch.OffContentTemplate>
    singleDataTemplate
  </ToggleSwitch.OffContentTemplate>
</ToggleSwitch>
Value
DataTemplate DataTemplate DataTemplate

The DataTemplate that displays the control's content while in "Off" state.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

OffContentTemplateProperty OffContentTemplateProperty OffContentTemplateProperty OffContentTemplateProperty

Identifies the OffContentTemplate dependency property.

public static DependencyProperty OffContentTemplateProperty { get; }public static DependencyProperty OffContentTemplateProperty { get; }Public Static ReadOnly Property OffContentTemplateProperty As DependencyPropertypublic static DependencyProperty OffContentTemplateProperty { get; }
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the OffContentTemplate dependency property.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

OnContent OnContent OnContent OnContent

Provides the object content that should be displayed using the OnContentTemplate when this ToggleSwitch has state of "On".

public object OnContent { get; set; }public object OnContent { get; set; }Public ReadWrite Property OnContent As objectpublic object OnContent { get; set; }
<ToggleSwitch OnContent="stringContent"/>
Value
object object object

The object content. In some cases this is a string, in other cases it is a single element that provides a root for further composition content. Probably the most common "set" usage is to place a binding here.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

OnContentProperty OnContentProperty OnContentProperty OnContentProperty

Identifies the OnContent dependency property.

public static DependencyProperty OnContentProperty { get; }public static DependencyProperty OnContentProperty { get; }Public Static ReadOnly Property OnContentProperty As DependencyPropertypublic static DependencyProperty OnContentProperty { get; }
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the OnContent dependency property.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

OnContentTemplate OnContentTemplate OnContentTemplate OnContentTemplate

Gets or sets the DataTemplate used to display the control's content while in "On" state.

public DataTemplate OnContentTemplate { get; set; }public DataTemplate OnContentTemplate { get; set; }Public ReadWrite Property OnContentTemplate As DataTemplatepublic DataTemplate OnContentTemplate { get; set; }
<ToggleSwitch>
  <ToggleSwitch.OnContentTemplate>
    singleDataTemplate
  </ToggleSwitch.OnContentTemplate>
</ToggleSwitch>
Value
DataTemplate DataTemplate DataTemplate

The DataTemplate that displays the control's content while in "On" state.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

OnContentTemplateProperty OnContentTemplateProperty OnContentTemplateProperty OnContentTemplateProperty

Identifies the OnContentTemplate dependency property.

public static DependencyProperty OnContentTemplateProperty { get; }public static DependencyProperty OnContentTemplateProperty { get; }Public Static ReadOnly Property OnContentTemplateProperty As DependencyPropertypublic static DependencyProperty OnContentTemplateProperty { get; }
Value
DependencyProperty DependencyProperty DependencyProperty

The identifier for the OnContentTemplate dependency property.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

TemplateSettings TemplateSettings TemplateSettings TemplateSettings

Gets an object that provides calculated values that can be referenced as TemplateBinding sources when defining templates for a ToggleSwitch control.

public ToggleSwitchTemplateSettings TemplateSettings { get; }public ToggleSwitchTemplateSettings TemplateSettings { get; }Public ReadOnly Property TemplateSettings As ToggleSwitchTemplateSettingspublic ToggleSwitchTemplateSettings TemplateSettings { get; }
Value
ToggleSwitchTemplateSettings ToggleSwitchTemplateSettings ToggleSwitchTemplateSettings

An object that provides calculated values for templates.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Methods

OnHeaderChanged(Object, Object) OnHeaderChanged(Object, Object) OnHeaderChanged(Object, Object) OnHeaderChanged(Object, Object)

Invoked when the content for Header changes.

protected virtual void OnHeaderChanged(Object oldContent, Object newContent)protected virtual void OnHeaderChanged(Object oldContent, Object newContent)Protected Overridable Function OnHeaderChanged(oldContent As Object, newContent As Object) As voidprotected virtual void OnHeaderChanged(Object oldContent, Object newContent)
Parameters
oldContent
System.Object System.Object System.Object

The string or object content of the old content.

newContent
System.Object System.Object System.Object

The string or object content of the new content.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

Override this method in order to add steps to behavior of a custom derived class, or to change the oldContent or newContent values before passing them on to base.

OnOffContentChanged(Object, Object) OnOffContentChanged(Object, Object) OnOffContentChanged(Object, Object) OnOffContentChanged(Object, Object)

Invoked when the content for OffContent changes.

protected virtual void OnOffContentChanged(Object oldContent, Object newContent)protected virtual void OnOffContentChanged(Object oldContent, Object newContent)Protected Overridable Function OnOffContentChanged(oldContent As Object, newContent As Object) As voidprotected virtual void OnOffContentChanged(Object oldContent, Object newContent)
Parameters
oldContent
System.Object System.Object System.Object

The string or object content of the old content.

newContent
System.Object System.Object System.Object

The string or object content of the new content that is about to display.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

Override this method in order to add steps to behavior of a custom derived class, or to change the oldContent or newContent values before passing them on to base.

OnOnContentChanged(Object, Object) OnOnContentChanged(Object, Object) OnOnContentChanged(Object, Object) OnOnContentChanged(Object, Object)

Invoked when the content for OnContent changes.

protected virtual void OnOnContentChanged(Object oldContent, Object newContent)protected virtual void OnOnContentChanged(Object oldContent, Object newContent)Protected Overridable Function OnOnContentChanged(oldContent As Object, newContent As Object) As voidprotected virtual void OnOnContentChanged(Object oldContent, Object newContent)
Parameters
oldContent
System.Object System.Object System.Object

The string or object content of the old content.

newContent
System.Object System.Object System.Object

The string or object content of the new content that is about to display.

Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

Override this method in order to add steps to behavior of a custom derived class, or to change the oldContent or newContent values before passing them on to base.

OnToggled() OnToggled() OnToggled() OnToggled()

Invoked before the Toggled event is raised.

protected virtual void OnToggled()protected virtual void OnToggled()Protected Overridable Function OnToggled() As voidprotected virtual void OnToggled()
Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

Override this method to provide a class-based behavior that should be invoked each time that the Toggled event is raised. The event data does not contain the state information, but you can determine the state from IsOn, which represents the new value.

Events

Toggled Toggled Toggled Toggled

Occurs when "On"/"Off" state changes for this ToggleSwitch.

public event RoutedEventHandler Toggledpublic event RoutedEventHandler ToggledPublic Event Toggledpublic event RoutedEventHandler Toggled
<ToggleSwitch Toggled="eventhandler"/>
Attributes
Additional features and requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)