We will look into three main types of in WPF.
This is the 1st Part from the series of Controls in WPF.
WPF provides total three types of controls.
- Content Controls
- Item Controls
- Layout Controls
We will look into each and every control's type in details as these are very basic controls but very much powerful. One who work in Windows Presentation Foundation (WPF) Technology should be aware about this controls in details to develop the applications more powerful.
Ok, so here we go.....
1. Content Controls
The Content Controls would be very much familiar to the people who are from Windows Forms background. In WPF, there are many controls which are originally Content Controls. This controls derives from the ContentControl Class. They can only have single nested element. The element can be accessed from the code using Content property.
VB.NET Code :
Button1.Content = "Hi, This is Button 1"
C#.NET Code :
button1.Content = "Hi, This is Button 1";
Here the Content would be an Object. So it can contain the values which an Object can. For example, in button control's content property, you can also set an Image.
Another point to note here is the Content Controls can have only one nested element, but they can have unlimited inherent elements. For example, if you have a button control, you can have StackPanel control inside button control also.
Let's take some example of the Content Controls:
The Button Control is known by everyone who codes in Microsoft Technologies. This control is been used to fire any event by clicking button (Remember, Button's Click is itself an event). The most important part of this control are it's IsDefault property and it's IsCancel property.
IsDefault property - If set to true, Button's event will be called when the Enter key is pressed.
IsCancel property - If set to true, Button's event will be called when the Esc key is pressed.
Button Control's Access Keys:
Access keys are very familiar to everyone who works on computer. When you press any alphabet with ALT key, a little underscore (_) sign will come up on button's caption attached to alphabet you pressed. For example, if you press X with ALT key, the X of Exit will come with underscore sign. This are called as mnemonics.
You can define mnemonics as follow,
Few other examples we have are like, CheckBoxes, RadioButtons, TexBlocks, Image Controls, etc,. I will cover one more Control here and I will leave rest for you to practice on as they are very much familiar.
So many person has asked me the same question including my team that What is a TextBlock Control?? It's nothing but a simple window which will represent the Text you have provided.
Huh!!! So why do we have label control??!!!!
Ok, Let's first understand the basic of TextBlock Control. The general example of the same would be as follow :
<TextBlock Name="MyText1">Welcome Guest</TextBlock>
Now, whenever you want to change the content of it through code, you can use the below code :
MyText1.Text = "Welcome " + UserName
One thing to notice here is the font of the TextBlock control will be the same as your window. You can change whenever you want.
Ok Ok Ok!!! But Why should I use it when I do have something nice as Label?????
The first thing to notice is TextBlock isn't a control!!!! (You kidding right!!) I do agree that the TextBlock appears in the System.Windows.Controls namespace but it is not a control. It is getting derived from FrameworkElement.
Ok, so what!!!! One should keep a note of few points on why should one prefer TextBlock than the Label.
When you disable a TextBlock, it will not turn into gray.
When you disable a Lable, it will turn into gray.
TextBlock doesn't support access keys for you caption.
When you want to have access keys for your caption, use Lable.
In short, If you just want to show some TEXT, user TextBlock.
Ok, so that's it for Content Controls. You may try using some other mentioned controls on your own and let us know if you find any difficulty, improvement, feature etc.
Thanks and Have Fun!!!!!