CheckBox Style

image_pdfimage_print


   
    
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="220" Width="300">
    <Window.Resources>
        <Style x:Key="BaseControlStyle" 
               TargetType="{x:Type Control}">
            <Setter Property="FontFamily" Value="Tahoma" />
            <Setter Property="FontSize" Value="24pt"/>
            <Setter Property="Margin" Value="4" />
        </Style>

        <Style TargetType="{x:Type Button}" 
               BasedOn="{StaticResource BaseControlStyle}">
            <Setter Property="FontWeight" Value="Bold" />
        </Style>

        <Style TargetType="{x:Type CheckBox}" 
               BasedOn="{StaticResource BaseControlStyle}">
        </Style>
        <Style TargetType="{x:Type TextBox}" 
               BasedOn="{StaticResource BaseControlStyle}">
        </Style>

    </Window.Resources>

    <Grid>
        <StackPanel>
            <CheckBox>CheckBox</CheckBox>
            <TextBox>TextBox</TextBox>
            <Button>Button</Button>
            <Button FontWeight="Light">Button with overridden style</Button>
            <TextBlock>TextBlock</TextBlock>
            <ComboBox>ComboBox</ComboBox>
        </StackPanel>
    </Grid>
</Window>

   
    
    
    
     


CheckBox checked event listener

image_pdfimage_print


   
  


<Window x:Class="LayoutPanels.LocalizableText"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Layout" Height="365" Width="380" MinWidth="350" MinHeight="150">
  <Grid>
    <StackPanel>
      <Button Name="cmdPrev" Margin="10,10,10,3">Prev</Button>
      <Button Name="cmdNext" Margin="10,3,10,3">Next</Button>      
      <CheckBox Name="chkLongText" Margin="10,10,10,10" Checked="chkLongText_Checked" Unchecked="chkLongText_Unchecked">Show Long Text</CheckBox>
    </StackPanel>    
    <TextBox  Margin="0,10,10,10" TextWrapping="WrapWithOverflow">
     This behavior makes localization much easier.</TextBox>
    <Button Grid.Row="1" Grid.Column="0" Name="cmdClose" Margin="10,3,10,10">Close</Button>
  </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LayoutPanels
{

    public partial class LocalizableText : Window
    {

        public LocalizableText()
        {
            InitializeComponent();
        }

        private void chkLongText_Checked(object sender, RoutedEventArgs e)
        {            
            cmdPrev.Content = " <- Go to the Previous Window ";
            cmdNext.Content = " Go to the Next Window -> ";
        }

        private void chkLongText_Unchecked(object sender, RoutedEventArgs e)
        {
            cmdPrev.Content = "Prev";
            cmdNext.Content = "Next";
        }
    }
}

   
    
     


Handles CheckBox Indeterminate events when a CheckBox changes to a indeterminate state.

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="250" Width="300">
    <StackPanel Name="panel">
        <CheckBox 
                  Content="Third CheckBox (Tri-State Enabled)" 
                  Indeterminate="CheckBox_Indeterminate"  IsChecked="True" 
                  IsThreeState="True" Margin="2" Name="checkbox3" 
                  />
        <Button Content="Get Selected" Margin="5" MaxWidth="100" 
                Click="Button_Click" />
        <TextBlock FontWeight="Bold" Text="Selected CheckBoxes:" />
        <ListBox Margin="5" MinHeight="2cm" Name="listbox" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            listbox.Items.Clear();
            foreach (CheckBox checkbox in panel.Children.OfType<CheckBox>().Where( cb => cb.IsChecked == true))
            {
                listbox.Items.Add(checkbox.Name);
            }
        }
        private void CheckBox_Indeterminate(object sender, RoutedEventArgs e)
        {
            if (!IsInitialized) return;

            CheckBox checkbox = e.OriginalSource as CheckBox;

            if (checkbox != null)
            {
                MessageBox.Show(checkbox.Name + " is indeterminate.", Title);
            }
        }

    }
}

   
    
     


Handle CheckBox Unchecked events

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="250" Width="300">
    <StackPanel Name="panel">
        <CheckBox Content="First CheckBox" 
                  IsChecked="True" Margin="2" Name="checkbox1" 
                  Unchecked="CheckBox_Unchecked"/>
        <CheckBox Content="Second CheckBox" 
                  IsChecked="False" Margin="2" Name="checkbox2" 
                  Unchecked="CheckBox_Unchecked"/>
        <Button Content="Get Selected" Margin="5" MaxWidth="100" 
                Click="Button_Click" />
        <TextBlock FontWeight="Bold" Text="Selected CheckBoxes:" />
        <ListBox Margin="5" MinHeight="2cm" Name="listbox" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            listbox.Items.Clear();
            foreach (CheckBox checkbox in panel.Children.OfType<CheckBox>().Where( cb => cb.IsChecked == true))
            {
                listbox.Items.Add(checkbox.Name);
            }
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (!IsInitialized) return;

            CheckBox checkbox = e.OriginalSource as CheckBox;

            if (checkbox != null)
            {
                MessageBox.Show(checkbox.Name + " is unchecked.", Title);
            }
        }
    }
}

   
    
     


Handle CheckBox checked events

image_pdfimage_print
   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="250" Width="300">
    <StackPanel Name="panel">
        <CheckBox Checked="CheckBox_Checked" Content="First CheckBox" 
                  IsChecked="True" Margin="2" Name="checkbox1"/>
        <Button Content="Get Selected" Margin="5" MaxWidth="100" 
                Click="Button_Click" />
        <TextBlock FontWeight="Bold" Text="Selected CheckBoxes:" />
        <ListBox Margin="5" MinHeight="2cm" Name="listbox" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            listbox.Items.Clear();
            foreach (CheckBox checkbox in panel.Children.OfType<CheckBox>().Where( cb => cb.IsChecked == true))
            {
                listbox.Items.Add(checkbox.Name);
            }
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsInitialized) return;
            CheckBox checkbox = e.OriginalSource as CheckBox;
            if (checkbox != null)
            {
                MessageBox.Show(checkbox.Name + " is checked.", Title);
            }
        }

    }
}

   
    
     


Use Linq to get checked CheckBox

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="250" Width="300">
    <StackPanel Name="panel">
        <CheckBox Checked="CheckBox_Checked" Content="First CheckBox" 
                  IsChecked="True" Margin="2" Name="checkbox1" 
                  />
        <Button Content="Get Selected" Margin="5" MaxWidth="100" 
                Click="Button_Click" />
        <TextBlock FontWeight="Bold" Text="Selected CheckBoxes:" />
        <ListBox Margin="5" MinHeight="2cm" Name="listbox" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            listbox.Items.Clear();
            foreach (CheckBox checkbox in panel.Children.OfType<CheckBox>().Where( cb => cb.IsChecked == true))
            {
                listbox.Items.Add(checkbox.Name);
            }
        }
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsInitialized) return;
            CheckBox checkbox = e.OriginalSource as CheckBox;
            if (checkbox != null)
            {
                MessageBox.Show(checkbox.Name + " is checked.", Title);
            }
        }


    }
}

   
    
     


Check the CheckBox based on key pressed states

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="170" Width="200">
    <StackPanel HorizontalAlignment="Center">
        <UniformGrid Columns="2">
            <UniformGrid.Resources>
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="IsHitTestVisible" Value="False" />
                    <Setter Property="Margin" Value="5" />
                </Style>
            </UniformGrid.Resources>
            <CheckBox Content="LeftControl" Name="chkLControl"/>
            <CheckBox Content="RightControl" Name="chkRControl"/>

        </UniformGrid>
        <Button Content="Check Keyboard" Margin="10" Click="Button_Click"/>
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            CheckKeyboardState();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CheckKeyboardState();
        }
        private void CheckKeyboardState()
        {
            chkLControl.IsChecked = Keyboard.IsKeyDown(Key.LeftCtrl);
            chkRControl.IsChecked = Keyboard.IsKeyDown(Key.RightCtrl);
            
        }
    }
}