WP7 ToggleSwitch in depth | key concepts and API
published on: 12/13/2010 | Views: N/A | Tags: WP7Toolkit
by WindowsPhoneGeek
In this article I am going to talk about the key properties, events and the main features of the Windows Phone 7 ToggleSwitch control in details..
One of the new components in the Silverlight Toolkit is ToggleSwitch. It is a UI element that enables users to turn something on or off. Basically it is mobile equivalent of the well known checkbox.
To begin using ToggleSwitch first add a reference to the Microsoft.Phone.Controls.Toolkit.dll assembly which is installed with the toolkit and you can find it in :
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Toolkit\Nov10\Bin\Microsoft.Phone.Controls.Toolkit.dll.
You will also need to add the "toolkit" prefix declaration. Make sure that your page declaration includes the "toolkit" namespace:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
The sample code should looks like:
<toolkit:ToggleSwitch x:Name="toggle" Content="ToggleSwitch is on" IsChecked="True" Header="ToggleSwitch"/>
Basic Structure
ToggleSwitch has two check states: On and Off.
Key Properties
ToggleSwitch derives from ContentControl so it exposes some properties like Content and ContentTemplate which come from the base class. In addition to that ToggleSwitch has the following properties:
- IsChecked
IsChecked is a dependency property of type bool?. It gets or sets whether the ToggleSwitch is checked.
Example:
<toolkit:ToggleSwitch x:Name="toggle" Content="ToggleSwitch is on" IsChecked="True" Header="ToggleSwitch"/>
- Header
Header is a dependency property of type object. It gets or sets the header of the ToggleSwitch.
- Content
An object that contains the control's content.
Example:
<toolkit:ToggleSwitch x:Name="toggle" Content="ToggleSwitch is on" IsChecked="True" Header="ToggleSwitch">
- HeaderTemplate
HeaderTemplate is a dependency property of type DataTemplate. It gets or sets the template used to display the control's header.
- ContentTemplate
The data template that is used to display the content of the ToggleSwitch.
Example:
<toolkit:ToggleSwitch Header="5:45 AM">
<toolkit:ToggleSwitch.HeaderTemplate>
<DataTemplate>
<ContentControl Foreground="{StaticResource PhoneForegroundBrush}" Content="{Binding}"/>
</DataTemplate>
</toolkit:ToggleSwitch. HeaderTemplate>
<toolkit:ToggleSwitch. ContentTemplate>
<DataTemplate>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Alarm: " FontSize="{StaticResource PhoneFontSizeSmall}"/>
<ContentControl HorizontalAlignment="Left" FontSize="{StaticResource PhoneFontSizeSmall}" Content="{Binding}"/>
</StackPanel>
<TextBlock Text="every schoolday" FontSize="{StaticResource PhoneFontSizeSmall}" Foreground="{StaticResource PhoneSubtleBrush}"/>
</StackPanel>
</DataTemplate>
</toolkit:ToggleSwitch. ContentTemplate>
</toolkit:ToggleSwitch>
- SwitchForeground
SwitchForeground is a dependency property of type Brush . It gets or sets the switch foreground color of the ToggleSwitch.
Example:
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Red);
Events
- Checked
Occurs when the ToggleSwitch" is checked.
Example:
this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked);
void toggle_Checked(object sender, RoutedEventArgs e)
{ //add some code here }
- Click
Occurs when the ToggleSwitch is clicked.
Example:
this.toggle.Click += new EventHandler<RoutedEventArgs>(toggle_Click);
void toggle_Click(object sender, RoutedEventArgs e)
{ //add some codehere }
- Indeterminate
Occurs when the ToggleSwitch" is indeterminate - neither on nor off
Example:
this.toggle.Indeterminate += new EventHandler<RoutedEventArgs>(toggle_Indeterminate);
void toggle_Indeterminate(object sender, RoutedEventArgs e)
{//add some code here }
- Unchecked
Occurs when the ToggleSwitch" is unchecked.
Example:
this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked);
void toggle_Unchecked(object sender, RoutedEventArgs e)
{//add some code here }
Sample Usage
Here is an example of how to use this control in the easiest way:
<toolkit:ToggleSwitch x:Name="toggle" Content="ToggleSwitch is on" IsChecked="True" Header="ToggleSwitch"/>
public MainPage()
{
InitializeComponent();
this.toggle.Checked += new EventHandler<RoutedEventArgs>(toggle_Checked);
this.toggle.Unchecked += new EventHandler<RoutedEventArgs>(toggle_Unchecked);
}
void toggle_Unchecked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "ToggleSwitch is off";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Red);
}
void toggle_Checked(object sender, RoutedEventArgs e)
{
this.toggle.Content = "ToggleSwitch is on";
this.toggle.SwitchForeground = new SolidColorBrush(Colors.Green);
}
The result can be seen on the following screen shots:
That was all about the Windows Phone 7 ToggleSwitch in depth. I hope that the article was helpful. You can find the full source code here:
You can also follow us on Twitter @winphonegeek
Comments
This is very out of date!!!
posted by: Mike on 10/9/2011 6:28:19 AM
The control is now called ToggleSwitchButton and no longer has the Header property (no idea what it is now though!) so just wanted to let people who find this to beware as it's out of date.
RE:This is very out of date
posted by: winphonegeek on 10/9/2011 1:07:21 PM
It seems that there is some misunderstanding. In order to clarify things here is an explanation:
The "ToggleSwitch" control is still available in the Windows Phone Toolkit Aug 2011. You can take a look at the official codeplex page: http://silverlight.codeplex.com/releases/view/71550
Although a "ToggleSwitchButton" class is also available in the source of the toolkit, it is a part of the Microsoft.Phone.Controls.Primitives namespace and is not supposed to be used as a control but rather as a building block (used in the ToggleSwitch control).
How to change border's color and background's color ?
posted by: Zepto on 10/12/2011 4:59:04 AM
How to change border's color and background's color in unchecked state? I tried but seems not working.
Red detailing not coming
posted by: Sam on 2/3/2012 8:16:02 AM
Hi,
I tried the above code. It works perfectly, expect that the red foreground color is not visible when the ToggleSwitch is unchecked.
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
