Introducing Templates

Templates enable you to completely customize the way a control or part of a control looks and are arguably one of XAML's most powerful features. Whereas styles enable you to define a group of property values that can be applied to a control or part of a control, templates enable you to completely redefine the XAML that forms its appearance. Because XAML enables a clear separation between look and behavior, maintaining only a contract between the two, as long as you fulfill the needs of the contract you can modify the control to look however you desire. These templates can be stored as resources and reused by multiple controls. There are two key types of templates in Silverlight: control templates and data templates.

A control template enables you to completely redefine the XAML for a control, hence completely changing its appearance. For example, you may want to customize a Button control to be round, instead of its default rectangular shape. You can define the XAML that accomplishes this alternative appearance as a control template and then apply this template to any Button controls that you want to look that way. Control templates are covered further in Chapters 9 and 12.

A data template enables you to define a custom way in which data should be displayed in a control. For example, you may want to customize the appearance of each item in a ComboBox control. Let's say the ComboBox is displaying a list of countries, but you also want to display the country's flag next to its name. To achieve this result, you can specify alternate XAML that the control will use to display each item as a data template. Data templates are covered further in Chapters 6, 7, and 10.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset