WPF CheckBox Control:
CheckBox in the user interface (UI) of WPF application is used to represent options that a user can select or clear. You can use a single check box or you can group two or more check boxes so that users can choose any number of options from a list of options.
In XAML CheckBox tag represents the CheckBox Control.
< CheckBox></ CheckBox>
Properties:
With the following tag I created the CheckBox, in which Height is height of the checkbox, Name is the nameof the checkbox. Background the the color of the box, Borderbrush is the color of the border, Forground is the color of the text.
<CheckBox Height="15" Margin="16,17,122,0" Name="chkA2zdotnet" VerticalAlignment="Top" Background="Cyan" BorderBrush="Blue" Foreground="Chocolate" FlowDirection="LeftToRight" HorizontalContentAlignment="Left" VerticalContentAlignment="Center">A2ZDotnet.com</CheckBox>
It would look like
Now if you want the checkbox to be on the right side instead of the left side set the FlowDirection property to "RightToLeft". Then it would look like.
Event:
CheckBox is derived from the ToggleButton so main event for the checkbox are Checked and Unchecked.
Add the following handler in the XAML
<CheckBox Height="15" Margin="16,17,122,0" Name="chkA2zdotnet" VerticalAlignment="Top" Background="Cyan" BorderBrush="Blue" Foreground="Chocolate" FlowDirection="RightToLeft" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" Padding="2,0,0,0"
Checked="chkA2zdotnet_OnChecked" Unchecked="chkA2zdotnet_OnUnchecked">A2ZDotnet.com</CheckBox>
Now write the chkA2zdotnet_OnChecked and chkA2zdotnet_OnUnchecked handler for the events.
protected void chkA2zdotnet_OnUnchecked(object sender, RoutedEventArgs e)
{
chkA2zdotnet.Background = Brushes.Cyan;
}
protected void chkA2zdotnet_OnChecked(object sender, RoutedEventArgs e)
{
chkA2zdotnet.Background = Brushes.LightPink;
}
IsChecked property is used to findout if a checkbox is checked or not as follows:
private void button1_Click(object sender, RoutedEventArgs e)
{
if (chkCShaprinterview.IsChecked == true) //checked
MessageBox.Show("http://www.a2zdotnet.com/QuestionsList.aspx is selected");
if (chkA2zdotnet.IsChecked == true) //checked
MessageBox.Show("chkA2zdotnet is selected");
if(chkContactUs.IsChecked == false) //not checked
MessageBox.Show("Contact Us is not selected");
}