Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
Visual Design
Controls
 How to: Handle the Checked Event fo...
Silverlight
How to: Handle the Checked Event for the CheckBox Control

When a user clicks a CheckBox control, one of three events can occur, depending on the current state of the CheckBox. Either the Checked, Unchecked or Indeterminate event occurs. You can handle these events and perform some action depending on the state of the check box.

The following code demonstrates how to handle the Checked and Indeterminate events.

<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<TextBlock Text="CheckBox Demonstration" Margin="0,20,10,20"
    FontFamily="Verdana" FontSize="18" FontWeight="Bold"
    Foreground="#FF5C9AC9" Grid.Row="0"/>

<!-- two state CheckBox -->
<CheckBox x:Name="cb1" Grid.Row="1" Content="Two State CheckBox" 
          Checked="HandleCheck" Unchecked="HandleUnchecked" Margin="5,0,0,0" />
<TextBlock x:Name="text1" Grid.Row="2" Margin="5,0,0,0" />

<!-- three state CheckBox -->
<CheckBox x:Name="cb2" Grid.Row="3" Content="Three State CheckBox" IsThreeState="True" Checked="HandleCheck" 
    Indeterminate="HandleThirdState" Unchecked="HandleUnchecked" Margin="5,10,0,0" />
<TextBlock x:Name="text2" Grid.Row="4" Margin="5,0,0,0" />


...


<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<TextBlock Text="CheckBox Demonstration" Margin="0,20,10,20"
    FontFamily="Verdana" FontSize="18" FontWeight="Bold"
    Foreground="#FF5C9AC9" Grid.Row="0"/>

<!-- two state CheckBox -->
<CheckBox x:Name="cb1" Grid.Row="1" Content="Two State CheckBox" 
          Checked="HandleCheck" Unchecked="HandleUnchecked" Margin="5,0,0,0" />
<TextBlock x:Name="text1" Grid.Row="2" Margin="5,0,0,0" />

<!-- three state CheckBox -->
<CheckBox x:Name="cb2" Grid.Row="3" Content="Three State CheckBox" IsThreeState="True" Checked="HandleCheck" 
    Indeterminate="HandleThirdState" Unchecked="HandleUnchecked" Margin="5,10,0,0" />
<TextBlock x:Name="text2" Grid.Row="4" Margin="5,0,0,0" />

C#
private void HandleCheck(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    if (cb.Name == "cb1")
        text1.Text = "Two state CheckBox checked.";
    else
        text2.Text = "Three state CheckBox checked.";
}

private void HandleUnchecked(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    if (cb.Name == "cb1")
        text1.Text = "Two state CheckBox unchecked.";
    else
        text2.Text = "Three state CheckBox unchecked.";
}

private void HandleThirdState(object sender, RoutedEventArgs e)
{
    CheckBox cb = sender as CheckBox;
    text2.Text = "Three state CheckBox indeterminate.";
}

Reference

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker