Home » WPF CheckBox Control

WPF CheckBox Control

by

WPF CheckBox Control

From the CheckBox Control we can select or unselect the item. CheckBox provides a list from where the user can select(check) the item. And we will use this list in most of the application. CheckBox control allows the user to toggle the option on or off. CheckBox shows us the value in the form of the Boolean value.

Properties used in the CheckBox

Here is the list of the properties used in the CheckBox.

From the CheckBox Control we can select or unselect the item. CheckBox provides a list from where the user can select(check) the item. And we will use this list in most of the application. CheckBox control allows the user to toggle the option on or off. CheckBox shows us the value in the form of the Boolean value.

Properties used in the CheckBox

Here is the list of the properties used in the CheckBox.

Sr. No.PropertyDescription
1.BackgroundThe background property is used to Gets or Sets the brush, which will provide us with background control.
2.BorderBrushBorderBrush property is used to Gets or Sets the brush, which we use to fill the border of the control.
3.ContentThe content property is used to gets or sets the content of the control.
4.ClickModeClickMode property is used to gets or sets the value when we click on the event according to the behavior of the device.

The method used in CheckBox

Here is the list of the methods used in the CheckBox.

Sr. No.PropertyDescription
1.ClearValueClearValue method is used to clear the local value of the dependency property.
2.FindNameWith the help of the FindName method, we will retrieve the object. The object contains a unique name.
3.OnContentChangedOnContentChanged method is used to invoke the value of the content property.
4.OnDragEnterOnDragEnter method is used to call when the DragEvent occurs.
5.SetBindingSetBinding method is used to attach the binding with the FrameworkElement by using the binding object.
6.OnToggleOnToggle method is used when the toggle button receives invoke instruction of the toggle.

Events used in CheckBox

Events used in CheckBox are as follows:

Sr. No.EventsDescription
1.CheckedWe will fire the Checked event when we check the ToggleButton. Checked event is inherited from the ToggleButton.
2.ClickClick event will occur when we click on the button. Click event is inherited from the ButtonBase.
3.DataContextChangedThis event will occur when the value of the FrameworkElement.DataContext property changes.
4.DragEnterDragEnter event will occur when the input system reports the drag event. The drag event will occur with the element as the target.
5.DragLeaveDragLeave event will occur when the input system reports the drag event with the element in the form of the origin.
6.DragOverDragOver event will occur when the input system reports the event in the potential drop of the target.
7.DragStartingDragStarting event will occur when the drag operation is initialized.
8.GotFocusGotFocus event will occur when the user interface element receives the focus.
9.IsEnabledChangeIsEnabledChange event will occur when the IsEnable property changes.
10.SizeChangedSizeChanges event will occur when the value of the actual height and ActualWidth changes on the FrameworkElement.
11.UncheckedUnchecked events will occur when we unchecked the toggle button.

Here we will explain how to create and use the CheckBox control in WPF with the help of the XAML and C#.

Creation of CheckBox

In XAML, checkbox element is shown by the WPF CheckBox control.

<CheckBox></CheckBox>

Here we will explain the features of the different attributes.

  • The content attribute will show the text of the CheckBox.
  • The name control shows the Name attribute; the name is known as the control’s unique identifier.
  • FontFamily, FontStyle, FontSize, FontStretch all these attributes are related to the font.

We will write the XAML code, through which we will create the CheckBox control and set the name of the checkbox, content, foreground, and font-related properties of the checkbox control. Here we will also show the functioning of the IsChecked attribute.

MainWindow.XAML

The output of the above code is shown below:

WPF CheckBox Control

In the above code, we defined the state of the IsChecked property in the checkbox control.

Add event handler in the CheckBox.

The checked and unchecked attribute of the checkbox element will add the checked and unchecked event handler. We will fire these events when the state of the CheckBox is changed.

The code for the event handler will look like as shown below:

MainWindow.XAML

OUTPUT

WPF CheckBox Control

When we click on the checkbox, the state of the checkbox is changed as shown in the below screenshot:

WPF CheckBox Control

Wrap Up

Here in the above, we have described the creation of the checkbox control at the time of the designing by using the WPF.


Next TopicWPF ComboBox

You may also like