WPF: XAML checkbox

If we have the checkbox in the XAML, it will be easier to format the checkbox as compared to using code to do the formatting.

MainWindow.xaml
<Window x:Class="CheckBox1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <CheckBox Name="cb" Checked="cb_checked" Unchecked="cb_unchecked" Margin="50, 50, 50, 50" FontSize="24" >Check Box</CheckBox>
    </Grid>
</Window>

MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace CheckBox1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        
        private void cb_checked(object sender, RoutedEventArgs e)
        {
            cb.Content = "Checked";
        }

        private void cb_unchecked(object sender, RoutedEventArgs e)
        {
            cb.Content = "Unchecked";
        }
    }
}

The output from running the above code will be as below.  You can try clicking on the checkbox.

xaml_checkbox

 

 

Series Navigation<< WPF: Checkbox status
WPF: Checkbox isChecked >>