Create a new window and write the following code:
[csharp]<Window x:Class="WPFCheckbox.Checkboxdatabind" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Checkboxdatabind" Height="300" Width="300">
<Grid Background="LightBlue">
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="161*" />
</Grid.RowDefinitions>
<TextBlock Text="India" Grid.Row="2" Margin="122,0,-122,0" FontWeight="Bold" />
<TextBlock Text="Australia" Grid.Row="3" Margin="122,0,-122,0" FontWeight="Bold"/>
<TextBlock Text="America" Grid.Row="4" Margin="122,0,-122,0" FontWeight="Bold" />
<CheckBox IsChecked="{Binding India}" Height="25" Width="80" Grid.Row="2"></CheckBox>
<CheckBox IsChecked="{Binding Australia}" Height="25" Width="80" Grid.Row="3"></CheckBox>
<CheckBox IsChecked="{Binding America}" Height="25" Width="80" Grid.Row="4"></CheckBox>
<Button Height="25" Width="100" Content="Submit" Grid.Row="5" Click="Button_Click"></Button>
</Grid>
</Window>
[/csharp]
Then the window should become like below :
Then go to code behind and write the following code:
[csharp]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WPFCheckbox
{
///
<summary>
/// Interaction logic for Checkboxdatabind.xaml
/// </summary>
public partial class Checkboxdatabind : Window
{
public Checkboxdatabind()
{
InitializeComponent();
this.DataContext=this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(string.Format("India: {0}, Australia: {1}, America: {2}", India, Australia, America));
}
public bool India { get; set; }
public bool Australia { get; set; }
public bool America { get; set; }
}
}
[/csharp]
Now run the application then following screen appears.
In above window, if India and Australia options are selected and click on
submit button, then the result appears as shown in below screen.