How
to use Checkbox in WPF........
In computing, a checkbox
is a graphical user interface element (widget) that permits the user to make a
binary choice, i.e. a choice between one of more possible mutually exclusive
options.
There are three property in Checkbox
- Checked
- Unchecked
- Trist ate Indeterminate
For Example:
Note:
1 In
this program We will discuss how to set
three property in checkbox
Code for Window4.xaml………………………….
<StackPanel Margin="10">
<Label FontWeight="Bold">Subject Options</Label>
<StackPanel Margin="10,5">
<CheckBox IsThreeState="True" Name="chkAll" Checked="chkAll_Checked" >Enable all</CheckBox>
<StackPanel Margin="20,5">
<CheckBox Name="chk1"
>.Net</CheckBox>
<CheckBox Name="chk2"
>Java</CheckBox>
<!--Unchecked............-->
<CheckBox Name="chk3" IsChecked="True">Php</CheckBox>
<!--Trist ate
Indeterminate-->
<CheckBox Name="chk4" IsChecked="{x:Null}">Android</CheckBox>
</StackPanel>
</StackPanel>
</StackPanel>
public partial class CheckBoxControl : Window
{
public CheckBoxControl()
{
InitializeComponent();
}
private void chkAll_Checked(object sender, RoutedEventArgs e)
{
bool t =
(chkAll.IsChecked == true);
chk1.IsChecked = t;
chk2.IsChecked = t;
chk3.IsChecked = t;
chk4.IsChecked = t;
}
}
Result:
Note:
2 in
this program we will discuss how to work with Enable All concept in checkbox
<Window x:Class="WpfApplicationControls.CheckBoxControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CheckBoxControl" Height="300" Width="300">
<Grid>
<StackPanel Margin="10">
<Label FontWeight="Bold">Subject Options</Label>
<StackPanel Margin="10,5">
<CheckBox IsThreeState="True" Name="chkAll" Checked="chkAll_Checked" Unchecked="chkAll_Unchecked_1" >Enable all</CheckBox>
<StackPanel x:Name="stpanel1" Margin="20,5">
<CheckBox Name="chk1"
>.Net</CheckBox>
<CheckBox Name="chk2"
>Java</CheckBox>
<CheckBox Name="chk3"
>Php</CheckBox>
<CheckBox Name="chk4"
>Android</CheckBox>
</StackPanel>
</StackPanel>
</StackPanel>
</Grid>
</Window>
using System;
using System.Text;
using System.Windows;
using
System.Windows.Controls;
namespace
WpfApplicationControls
{
/// <summary>
/// Interaction logic
for CheckBoxControl.xaml
/// </summary>
public partial class CheckBoxControl : Window
{
public CheckBoxControl()
{
InitializeComponent();
}
//When ChkAll is Checked
.....
private void chkAll_Checked(object sender, RoutedEventArgs e)
{
if (chkAll.IsChecked
== true)
{
foreach (UIElement ul in stpanel1.Children)
{
if (ul is CheckBox)
{
CheckBox chk = (CheckBox)ul;
chk.IsChecked = true;
}
}
}
}
//When ChkAll is UnChecked
.....
private void chkAll_Unchecked_1(object sender, RoutedEventArgs e)
{
foreach (UIElement ul in stpanel1.Children)
{
if (ul is CheckBox)
{
CheckBox chk = (CheckBox)ul;
chk.IsChecked = false;
}
}
}
}
}
Result:2
Note:
3 in
this program we will discuss how to get checked value of Checkbox in WPF
Code for Window4.xaml………………………….
<Window x:Class="WpfApplicationControls.Window4"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window4" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="CheckBox in W
P F"
TextAlignment="Center" FontSize="20" Foreground="Red" Padding="3,0,25,35"></TextBlock>
<Label Grid.Row="1" Grid.Column="0" Content="Name" Padding="30,10,10,3"></Label>
<Label Grid.Row="2" Grid.Column="0" Content="Course" Padding="30,10,10,3"></Label>
<TextBox Grid.Row="1" Grid.Column="1" Margin="10,5,6,6" Width="150"></TextBox>
<StackPanel Grid.Row="2" x:Name="spforcheckbox" Grid.Column="1" Width="150" Height="125" Background="Azure"
>
<CheckBox Margin="4" Content="High
School"
IsChecked="True"
/>
<CheckBox Margin="4" Content="Intermidiate"
/>
<CheckBox Margin="4" Content="Graduation"
/>
<CheckBox Margin="4" Content="Post
Graduation" />
</StackPanel>
<Button Grid.Row="3" Grid.Column="0" Content="GetValue" x:Name="btngetvalue" Margin="5,5,5,5" Click="btngetvalue_Click"
></Button>
<Label Grid.Row="3" Grid.Column="1" x:Name="lblmessage" Margin="5,5,5,5"
></Label>
</Grid>
</Window>
Code for
Window4.xaml………………………….
using System;
using System.Text;
using System.Windows;
using
System.Windows.Controls;
namespace
WpfApplicationControls
{
/// <summary>
/// Interaction logic
for Window4.xaml
/// </summary>
public partial class Window4 : Window
{
public Window4()
{
InitializeComponent();
}
string res;
//get checked value from
CheckBox which is located in Stack Panel…………….
private void btngetvalue_Click(object sender, RoutedEventArgs e)
{
res = "";
foreach (UIElement ul in spforcheckbox.Children)
{
if (ul is CheckBox)
{
CheckBox chk =(CheckBox)ul;
if (chk.IsChecked==true)
{
res += chk.Content + ",";
}
}
}
lblmessage.Content =
res.Remove(res.Length - 1);
}
}
}
Result: