The ToggleButton is a Silverlight control which allows the user to change its state. It is the base class for CheckBox andRadioButton controls but it can also be used as a standalone control.
Overview
To demonstrate the use of the ToggleButton I’ll give an example:
<UserControl x:Class="ToggleButton2.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="200" Height="100">
<Canvas x:Name="cnvLayoutRoot" Background="White">
<TextBlock x:Name="tblText" Canvas.Top="20" Canvas.Left="30" Text="Not clicked yet."></TextBlock>
<ToggleButton x:Name="tbtnTest" Checked="tbtnTest_Checked" Unchecked="tbtnTest_Unchecked"Canvas.Top="60" Canvas.Left="30" Content="Change state"></ToggleButton>
</Canvas>
</UserControl>private void tbtnTest_Checked( object sender, RoutedEventArgs e )
{
this.tblText.Text = "Checked";
}
private void tbtnTest_Unchecked( object sender, RoutedEventArgs e )
{
this.tblText.Text = "Unchecked";
}An interesting property is the IsThreeState. When it is set to true the ToggleButton has one more state – Indetermined. When the button is in this state the IsChecked property is null. The following example demonstrates it:
XAML:
<ToggleButton x:Name="tbtnTest" IsThreeState="True" Indeterminate="tbtnTest_Indeterminate"Checked="tbtnTest_Checked" Unchecked="tbtnTest_Unchecked" Canvas.Top="60" Canvas.Left="30"Content="Change state"></ToggleButton>
Code behind: private void tbtnTest_Indeterminate( object sender, RoutedEventArgs e )
We just set this property to true and attached to the Indeterminate event in order to change the TextBlock’s text to correspond to the state of the ToggleButton.{
this.tblText.Text = "Indetermined";
}Here the result is:
Example
As an example I’ll give the following simple gender control:
Here the XAML is:
<UserControl x:Class="ToggleButton2.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="200" Height="100">
<Canvas x:Name="cnvLayoutRoot" Background="White">
<ToggleButton x:Name="tbtnGender" Checked="tbtnGender_GenderChanged"Unchecked="tbtnGender_GenderChanged" Indeterminate="tbtnGender_GenderChanged" IsThreeState="True"Canvas.Top="35" Canvas.Left="20" Content="Gender: "></ToggleButton>
<TextBlock x:Name="tblGender" Canvas.Top="40" Canvas.Left="80"></TextBlock>
</Canvas>
</UserControl>Here is the code behind:
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
this.tbtnGender.IsChecked = null;
this.SetGenderText();
}
private void tbtnGender_GenderChanged( object sender, RoutedEventArgs e )
{
this.SetGenderText();
}
private void SetGenderText()
{
switch ( this.tbtnGender.IsChecked )
{
case null:
this.tblGender.Text = "Not specified";
break;
case true:
this.tblGender.Text = "Male";
break;
case false:
this.tblGender.Text = "Female";
break;
}
}
}Conclusion
This article is just a brief description of the key features of the ToggleButton control. It targets the developer who has just started with the new Silverlight 2 controls. Any comments are welcome.
Download Source Code










