Checkbox control in WPF
In the world of programming sometimes we get a situation where we want to checked some items depending upon requirement such as when we make any program related with employee registration then we checked employee qualification, language knowledge etc. To fulfill these requirements WPF provides a control named check box control which is used to check certain items depending upon users requirements. In this demonstration I will show you how to use check box control in WPF and explain some common properties and events of the check box control.
Some properties related with Check box control
Ø Name—Name property is used to provide a unique name to your check box control.
Ø Content—Content property is used to provide some content to the check box control.
Ø FontFamily—FontFamily property is used to change font family of your control such as Times New Roman, Lucida etc.
Ø FontSize—FontSize property is used to change font size of text of your check box control.
Ø FontStyle—FontStyle property is used to change font style of content of check box control.
Ø FlowDirection—FlowDirection property is used to set direction of the content in check box control. It has two properties LeftToRight and RightToLeft.
Some events associated with Check Box control
Ø Checked—Checked event is fired when ever any user checked the check box control.
Ø Unchecked—Unchecked event is fired when ever any user unchecked the check box control.
The following code represents a checkbox control and set its properties
<CheckBox x:Name="CheckBox1" Margin="70,70"
Content="Check Box Demo" FontFamily="Consolas"
Checked="CheckBox1_Checked" Unchecked="CheckBox1_Unchecked" />
<TextBlock x:Name="Message1" Margin="70,100,100,40" >
Output of the above code snippet is as follows
Whenever user checked check box control then Unchecked State message is changed to checked state and again when user unchecked the check box message Unchecked state is displayed.
Creating checkbox control dynamically
Now in further section I am going to explain you that how can we create a check box control dynamically.
/// In this code snipped i will show you that how can we create a checbox control
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Loaded(object sender, RoutedEventArgs e)
CheckBox chb = new CheckBox(); //Creating an object of check box control.
chb.Content = "Check Box Demo"; //Adding content to the check box control.
chb.IsChecked = false; //Unchecked check box control.
chb.FlowDirection = FlowDirection.RightToLeft; //Changing the flow direction of the check box control.
chb.Margin = new Thickness(30, 60, 100, 30); //Setting the margin of the control.
this.Content = chb; //Adding checkbox control to the window control.