
Namespace: System.Windows.Controls
Library: PresentationFramework
A Checkbox can be thinked like a Button. You don't click it, you check it.
We recreate the previous demo. Look the code:
The Window1.xaml file is:
<Window
xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005"
x:Class="DMU.Window1"
Title="DMU Tutorials Serie"
Top="100"
Left= "100"
Width="400"
Height="400"
>
<Canvas Width="400" Height="400" Background="DeepSkyBlue" >
<TextBlock Name="Text1" Canvas.Top="50" Canvas.Left="100" FontSize="14">Check boxes to populate list</TextBlock>
<CheckBox Name="Bt1" Canvas.Top="100" Canvas.Left="100" Checked = "Hat">Hat</CheckBox>
<CheckBox Name="Bt2" Canvas.Top="100" Canvas.Left="200" Checked = "Shoe">Shoe</CheckBox>
<CheckBox Name="Bt3" Canvas.Top="100" Canvas.Left="300" Checked = "Shirt">Shirt</CheckBox>
<ListBox Name="TheLB" Canvas.Top="150" Canvas.Left="100" Width="100" Height="55" SelectionChanged="Print" >
<ListBoxItem>Trousers</ListBoxItem>
</ListBox>
<TextBlock Canvas.Top="250" Canvas.Left="100" FontSize="14" >Click one to select</TextBlock>
<TextBox Name = "TheItem" Canvas.Top="300" Canvas.Left="100" FontSize="14"/>
</Canvas>
</Window>
And the Window1.xaml.cs file is:
namespace DMU{
public partial class Window1 : Window{
public Window1(){
InitializeComponent();
}
private void Hat(object sender, RoutedEventArgs e){
ListBoxItem li = new ListBoxItem();
li.Content = "Hat" ;
TheLB.Items.Add(li);
Bt1.IsEnabled = false;
}
private void Shoe(object sender, RoutedEventArgs e){
ListBoxItem li = new ListBoxItem();
li.Content = "Shoe" ;
TheLB.Items.Add(li);
Bt2.IsEnabled = false;
}
private void Shirt(object sender, RoutedEventArgs e){
ListBoxItem li = new ListBoxItem();
li.Content = "Shirt";
TheLB.Items.Add(li);
Bt3.IsEnabled = false;
}
void Print(object sender, SelectionChangedEventArgs args)
{
ListBoxItem lboxitem = new ListBoxItem();
lboxitem = TheLB.SelectedItem as ListBoxItem;
TheItem.Text = "Selected: " + lboxitem.Content.ToString();
}
}
}
IMPORTANT: We are not presenting the other 3 usual files of the application. Pay attention how we are doing a conversion (as ListBoxItem). This works for some types.
You can run the application. Please, you need to liberate the firewall.
![]() |
After this introduction, some properties:
Background
The value will be a color.
BorderBrush
Also a color.
BorderThickness
Click
If the CheckBox is checked, unchecks it; and vice-versa. Changes the property IsChecked.
Height
IsChecked
You need to use True | False. It's a read/write property.
IsEnabled
Margin
Name
Opacity
Visibility
Width
Some events. To be overrided.
The more important (you can see how to use it at the sample):
Checked
There are a:
Unchecked
KeyDown
KeyUp
And the usual mouse events:
MouseDoubleClick MouseDown MouseEnter MouseLeave MouseLeftButtonDown MouseLeftButtonUp MouseMove MouseRightButtonDown MouseRightButtonUp MouseUp
PREVIOUS LESSON NEXT LESSON T.CONTENTS HOMEPAGE
