Control Template

There are 2 types of WPF templates.

  1. Control template : This template is used to change the look and feel of the control.
  2. Data Template : This template is used to define the presentation of data.


Control Template :

WPF has its advantage in visual appearance. We can have round button, triangular button or any shape by changing the template of button. Similarly we can change the look and feel of any control by using templates. This is where Control template is used.

This is how I understood how ControlTemplate works.

Step 1 :

Draw a button.







Step 2:
Suppose we want the button to be in round shape, we have to use Ellipse within Control template.
We can use Ellipse for the same and can put the Height and Width same.
   
                   








Step 3 :
Now the content "Hi Hello" is not visible. Once we set the Ellipse as the control template , the content is not rendered. To make it visible we have to use ContentPresenter.
Since the ControlTemplate can have only one child we will put Ellipse and ContentPresenter inside a Grid.

                      










If we don't put VerticalAlignment and HorizontalAlignment as Center, the text might go to the Top as shown in the right picture.













What is ContentPresenter ?
To specify where the control has to be added in ControlTemplate ContentPresenter is used.
Every ControlTemplate will have a ContentPresenter .

For more information read
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentpresenter.aspx

No comments:

Post a Comment

Pages