ToggleSwitch inherits directly from System.Windows.Control.

The dependency property IsOn determines whether a ToggleSwitch is on or off. Two bubbling routed events, ToggledOn and ToggledOff, provide notification when the value of IsOn changes.

ToggleSwitch can be oriented either horizontally or vertically. Determined by the Orientation property.

On/Off Content and the Header

Properties OnContent and OffContent define what is displayed by a ToggleSwitch when switched on and when switched off, respectively. Both are type Object. There are corresponding DataTemplate properties, OnContentTemplate and OffContentTemplate, which define how the content values are rendered. If the data templates are not used, then the content’s display value is obtained from its ToString method.

The Header property specifies a header for a ToggleSwitch. There is a corresponding HeaderTemplate property.

Placement of both the On/Off content and Header can be modified. For the On/Off content, use ContentPlacement, which is an enumeration of type ToggleSwitchContentPlacement with possible values Center, Left, Right, Top and Bottom. A value of Center causes the on/off content to be placed inside the switch track. The other four values are relative to the switch track. For placement of the header, use HeaderPlacement. It is an enumeration of type Dock with possible values Left, Right, Top and Bottom.

Further control of the rendering of on/off content can be achieved via properties HorizontalContentAlignment, VerticalContentAlignment and Padding. The corresponding properties for the header are HorizontalHeaderAlignment, VerticalHeaderAlignment and HeaderPadding.

Sizing

MinHeight and MinWidth determine the minimum width and height of the switch’s track, not the minimum width and height of the ToggleSwitch as a whole. If ContentPlacement is Center, then MinHeight and MinWidth are automatically adjusted, if necessary, to accommodate the size of the rendered on/off content. You can also affect the switch’s size with the Padding property, if ContentPlacement is Center.

Customization

Customization of various brushes used by the template are possible by providing alternate values for resources that are given pre-defined keys. Here is an example:

<Thickness x:Key="{x:Static dc:ToggleSwitch.BorderThicknessKey}">2</Thickness>
<
Thickness x:Key="{x:Static dc:ToggleSwitch.InnerBorderThicknessKey}">1</Thickness>

<SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.BackgroundKey}">#FFFFFF</SolidColorBrush
>
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.BorderBrushKey}">#A6A6A6</SolidColorBrush
>
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.InnerBorderBrushKey}">Transparent</SolidColorBrush
>
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.HoverBorderBrushKey}" Color="#989898" />
       
<SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.OnBackgroundKey}">#4617B4</SolidColorBrush
>
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.OffBackgroundKey}">#A6A6A6</SolidColorBrush
>
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.OnHoverBackgroundKey}">#5F37BE</SolidColorBrush
>
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.OffHoverBackgroundKey}">#B5B5B5</SolidColorBrush>

<SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.KnobBackgroundKey}">#000000</SolidColorBrush
>
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.KnobBorderBrushKey}">#000000</SolidColorBrush
>
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.KnobHoverBorderBrushKey}" Color
="#000000" />
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.KnobHoverBackgroundKey}" Color
="#444444" />
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.KnobPressedBorderBrushKey}" Color
="#000000" />
<
SolidColorBrush x:Key="{x:Static dc:ToggleSwitch.KnobPressedBackgroundKey}" Color="#000000"
/>

Further Exploration

The best place to explore the ToggleSwitch, other than this document, is the sample ToggleSwitchSample which is provided with the installation of DotNetBar. Here is a screen shot of the sample application running in Windows 8.1:

ToggleSwitch

Related posts:

  1. WPF Color Control Quick Start Guide
  2. WPF RadialMenu Quick Start Guide
  3. ProgressSteps Quick Start Guide, DotNetBar for WPF
  4. Circular Progress Control (WPF) Quick Start Guide
  5. AdvTextBox Quick Start Guide, DotNetBar for WPF