WPF - Checkbox

A checkbox is a control that a user can select (check) or clear (uncheck). It provides a list of options that a user can select, such as a list of settings to apply to an application. The hierarchical inheritance of Checkbox class is as follows −
Background
Gets or sets a brush that provides the background of the control. (Inherited from Control)
BorderBrush
Gets or sets a brush that describes the border fill of a control. (Inherited from Control)
BorderThickness
Gets or sets the border thickness of a control. (Inherited from Control)
Content
Gets or sets the content of a ContentControl. (Inherited from ContentControl)
ClickMode
Gets or sets a value that indicates when the Click event occurs, in terms of device behavior. (Inherited from ButtonBase)
ContentTemplate
Gets or sets the data template that is used to display the content of the ContentControl. (Inherited from ContentControl)
FontFamily
Gets or sets the font used to display text in the control. (Inherited from Control)
FontSize
Gets or sets the size of the text in this control. (Inherited from Control)
FontStyle
Gets or sets the style in which the text is rendered. (Inherited from Control)
FontWeight
Gets or sets the thickness of the specified font. (Inherited from Control)
Foreground
Gets or sets a brush that describes the foreground color. (Inherited from Control)
Height
Gets or sets the suggested height of a FrameworkElement. (Inherited from FrameworkElement)
HorizontalAlignment

Gets or sets the horizontal alignment characteristics that are applied to a FrameworkElement when it is composed in a layout parent, such as a panel or items control. (Inherited from FrameworkElement)
IsChecked
Gets or sets whether the ToggleButton is checked. (Inherited from ToggleButton)
IsEnabled
Gets or sets a value indicating whether the user can interact with the control. (Inherited from Control)
IsPressed
Gets a value that indicates whether a ButtonBase is currently in a pressed state. (Inherited from ButtonBase)
17
IsThreeState
Gets or sets a value that indicates whether the control supports three states. (Inherited from ToggleButton)
Margin
Gets or sets the outer margin of a FrameworkElement. (Inherited from FrameworkElement)
Name
Gets or sets the identifying name of the object. When a XAML processor creates the object tree from XAML markup, run-time code can refer to the XAML-declared object by this name. (Inherited from FrameworkElement)
Opacity
Gets or sets the degree of the object's opacity. (Inherited from UIElement)
Resources
Gets the locally defined resource dictionary. In XAML, you can establish resource items as child object elements of a frameworkElement. Resources property element, through XAML implicit collection syntax. (Inherited from FrameworkElement)
Style
Gets or sets an instance Style that is applied for this object during layout and rendering. (Inherited from FrameworkElement)
Template
Gets or sets a control template. The control template defines the visual appearance of a control in UI, and is defined in XAML markup. (Inherited from Control)
VerticalAlignment
Gets or sets the vertical alignment characteristics that are applied to a FrameworkElement when it is composed in a parent object such as a panel or items control. (Inherited from FrameworkElement)
Visibility
Gets or sets the visibility of a UIElement. A UIElement that is not visible is not rendered and does not communicate its desired size to layout. (Inherited from UIElement)
Width
Gets or sets the width of a FrameworkElement. (Inherited from FrameworkElement)
ClearValue
Clears the local value of a dependency property. (Inherited from DependencyObject)
FindName
Retrieves an object that has the specified identifier name. (Inherited from FrameworkElement)
OnApplyTemplate
Invoked whenever application code or internal processes (such as a rebuilding layout pass) call ApplyTemplate. In simplest terms, this means the method is called just before a UI element displays in your app. Override this method to influence the default post-template logic of a class. (Inherited from FrameworkElement)
OnContentChanged
Invoked when the value of the Content property changes. (Inherited from ContentControl)
OnDragEnter
Called before the DragEnter event occurs. (Inherited from Control)
OnDragLeave
Called before the DragLeave event occurs. (Inherited from Control)
OnDragOver
Called before the DragOver event occurs. (Inherited from Control)
OnDrop
Called before the Drop event occurs. (Inherited from Control)
OnGotFocus
Called before the GotFocus event occurs. (Inherited from Control)
OnKeyDown
Called before the KeyDown event occurs. (Inherited from Control)
OnKeyUp
Called before the KeyUp event occurs. (Inherited from Control)
OnLostFocus
Called before the LostFocus event occurs. (Inherited from Control)
OnToggle
Called when the ToggleButton receives toggle stimulus. (Inherited from ToggleButton)
SetBinding
Attaches a binding to a FrameworkElement, using the provided binding object. (Inherited from FrameworkElement)
Checked
Fires when a ToggleButton is checked. (Inherited from ToggleButton)
Click
Occurs when a button control is clicked. (Inherited from ButtonBase)
DataContextChanged
Occurs when the value of the FrameworkElement.DataContext property changes. (Inherited from FrameworkElement)
DragEnter
Occurs when the input system reports an underlying drag event with this element as the target. (Inherited from UIElement)
DragLeave
Occurs when the input system reports an underlying drag event with this element as the origin. (Inherited from UIElement)
DragOver
Occurs when the input system reports an underlying drag event with this element as the potential drop target. (Inherited from UIElement)
DragStarting
Occurs when a drag operation is initiated. (Inherited from UIElement)
GotFocus
Occurs when a UIElement receives focus. (Inherited from UIElement)
Holding
Occurs when an otherwise unhandled Hold interaction occurs over the hit test area of this element. (Inherited from UIElement)
Intermediate
Fires when the state of a ToggleButton is switched to the indeterminate state. (Inherited from ToggleButton)
IsEnabledChanged
Occurs when the IsEnabled property changes. (Inherited from Control)
KeyDown
Occurs when a keyboard key is pressed while the UIElement has focus. (Inherited from UIElement)
KeyUp
Occurs when a keyboard key is released while the UIElement has focus. (Inherited from UIElement)
LostFocus
Occurs when a UIElement loses focus. (Inherited from UIElement)
SizeChanged
Occurs when either the ActualHeight or the ActualWidth property changes value on a FrameworkElement. (Inherited from FrameworkElement)
Unchecked
Occurs when a ToggleButton is unchecked. (Inherited from ToggleButton)
Let’s create a new WPF project with the name WPFCheckBoxControl.
Let’s create a new WPF project with the name WPFCheckBoxControl.
Drag two checkboxes and two textboxes from a toolbox and set the following properties in the properties window.
Drag two checkboxes and two textboxes from a toolbox and set the following properties in the properties window.
Now switch to XAML window in which you will see the XAML tags for check boxes and text boxes.
Now switch to XAML window in which you will see the XAML tags for check boxes and text boxes.
Add some more properties and selection event as shown in the following XAML code.
Add some more properties and selection event as shown in the following XAML code.
<Window x:Class = "WPFCheckBoxControl.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" 
   xmlns:local = "clr-namespace:WPFCheckBoxControl" 
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604"> 
	
   <Grid> 
      <CheckBox x:Name = "checkBox1" Content = "Two States" HorizontalAlignment = "Left" 
         Margin = "80,70,0,0" VerticalAlignment = "Top" Checked = "HandleCheck" 
         Unchecked = "HandleUnchecked" Width = "90"/>
			
      <CheckBox x:Name = "checkBox2" Content = "Three States" 
         HorizontalAlignment = "Left" Margin = "80,134,0,0" VerticalAlignment = "Top" 
         Width = "90" IsThreeState = "True" Indeterminate = "HandleThirdState" 
         Checked = "HandleCheck" Unchecked = "HandleUnchecked"/>
			
      <TextBox x:Name = "textBox1" HorizontalAlignment = "Left" 
         Height = "23" Margin = "236,68,0,0" TextWrapping = "Wrap"
         VerticalAlignment = "Top" Width = "300"/> 
			
      <TextBox x:Name = "textBox2" HorizontalAlignment = "Left"
         Height = "23" Margin = "236,135,0,0" TextWrapping = "Wrap" 
         VerticalAlignment = "Top" Width = "300"/> 
   </Grid> 
	
</Window>
Here is the implementation in C# for different events −
using System.Windows; 
using System.Windows.Controls;

namespace WPFCheckBoxControl { 
   /// <summary> 
      /// Interaction logic for MainWindow.xaml 
   /// </summary> 
	
   public partial class MainWindow : Window {
	
      public MainWindow() { 
         InitializeComponent(); 
      } 
		
      private void HandleCheck(object sender, RoutedEventArgs e) { 
         CheckBox cb = sender as CheckBox;
			
         if (cb.Name == "checkBox1") textBox1.Text = "2 state CheckBox is checked."; 
         else textBox2.Text = "3 state CheckBox is checked."; 
      }
		
      private void HandleUnchecked(object sender, RoutedEventArgs e) { 
         CheckBox cb = sender as CheckBox; 
			
         if (cb.Name == "checkBox1") textBox1.Text = "2 state CheckBox is unchecked."; 
         else textBox2.Text = "3 state CheckBox is unchecked."; 
      }
		
      private void HandleThirdState(object sender, RoutedEventArgs e) { 
         CheckBox cb = sender as CheckBox; 
         textBox2.Text = "3 state CheckBox is in indeterminate state."; 
      } 
		
   } 
}
When you compile and execute the above code, it will produce the following window. When you check / uncheck the checkboxes, it will update the text block accordingly.
We recommend that you execute the above example code and try some other properties and events of the checkbox control.

    31 Lectures 
     2.5 hours 

 Anadi Sharma

    30 Lectures 
     2.5 hours 

 Taurius Litvinavicius
 Print
 Add Notes
 Bookmark this page