:: Home

  login:         
  passwords:  

WPF FAQs

WPF General Questions
Concepts Element Tree
Concepts Dependency Property
Concepts Attached Property
Concepts Routed Events
Concepts Resources.
Concepts Animation
Concepts Freezable Object
Concepts Input and Commands.
Concepts Layouts
XAML Inline Styles and Templates
XAML XML Namespaces
XAML Code Behind
XAML Custom Class
XAML Type Converters
Content Model ItemControl
Content Model HeaderedItemsControl
Content Model HeaderedContentControl
Content Model Content Control
Content Model Decorator Content Model
Content Model Panel Content Model
Documents Serialization and Storage
Documents Annotations
Documents Flow Content
Documents Printing
Graphics and Multimedia Rendering Graphics
Graphics and Multimedia Animation and Timing
Graphics and Multimedia 2D Graphics
Graphics and Multimedia 3D Graphics
Graphics and Multimedia Visual Layer
Control Customization Adorners
Control Customization Stylable controls
Control Customization ControlTemplates
Data Data Binding
Data DragandDrop
Data Serialization
Globalization and Localization Attributes
Globalization and Localization Comments
Globalization and Localization Globalization Struc
Application and Deployment ClickOnce
Application and Deployment Frame
Application and Deployment Page
Application and Deployment Navigation
Application and Deployment Setup
Application and Deployment Window
Interoperability Message Loops Between
Interoperability Win32 in WPF
Interoperability WPF in Win32
Interoperability Windows Forms and WPF
Security Trusted Security
Security Partial Trust Security
Tools Microsoft Expression Blend
Tools ZAM3D
Tools XAMLPAD
Controls ToolTip
Controls TextBlock
Controls TabControl
Controls ProgressBar
Controls PrintDialog
Controls Popup
Controls TextBox
Controls Canvas
Controls ComboBox
Controls ListBox
Controls StatusBar
Controls ToolBar
Controls Context Menu
Controls Expander
Controls DocumentViewer
Controls FlowDocumentReader
Controls GroupBox
Controls GridSplitter
Controls Image
Controls Menu
Controls ScrollBar
Controls Slider
Controls RichTextBox
Controls Border
Controls Buttons

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

Winforms Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

Control Customization ControlTemplates Interview Questions & FAQs

Questions and answers for Control Customization ControlTemplates.
 

33. Control Customization ControlTemplates

    33.1 What is the use of a ControlTemplate ?
    33.2 How to access elements that are embedded inside the ControlTemplate ?
    33.3 How do I build an ItemsControl ControlTemplate ?
    33.4 How to edit templates using Microsoft Expression Blend ?
    33.5 How to get the default template of a control programmatically ?

33.1 What is the use of a ControlTemplate ?

It allows you to specify the visual structure of a class. The main advantage is we can override the default ControlTemplate defined in the control and replace it with new ControlTemplates, to reconstruct the visual structure of the class.

The following code illustrates the ControlTemplate of a button.

[XAML]

<Style TargetType="Button">

<!--Set to true to not get any properties from the themes.-->

<Setter Property="OverridesDefaultStyle" Value="True"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="Button">

<Grid>

<Ellipse Fill="{TemplateBinding Background}"/>

<ContentPresenter HorizontalAlignment="Center"

VerticalAlignment="Center"/>

</Grid>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>


33.2 How to access elements that are embedded inside the ControlTemplate ?

When you declare elements inside the ControlTemplate, use the "Name" of each control for identifying if from the outside. We can then use the "FindName" function for finding the resource.

An example shown below is for accessing the TextBlock of a button.

[XAML]

<Button Background="LightGoldenrodYellow">

     <Button.Style>

          <Style TargetType="Button">

          <!--Set to true to not get any properties from the themes.-->

               <Setter Property="OverridesDefaultStyle" Value="True"/>

               <Setter Property="Template">

               <Setter.Value>

                    <ControlTemplate TargetType="Button">

                    <Grid>

                    <Ellipse Name="innerEllipse" Fill="{TemplateBinding Background}"/>

                         <ContentPresenter HorizontalAlignment="Center"

               VerticalAlignment="Center"/>

                    </Grid>

                    </ControlTemplate>

               </Setter.Value>

               </Setter>

          </Style>

     </Button.Style>

</Button>

 
[C#]

Ellipse ellipse = this.Template.FindName("innerEllipse", this) as Ellipse;

33.3 How do I build an ItemsControl ControlTemplate ?

he important key point when building an ItemsControl or any of it's derived classes (listbox, combobox, listview etc.), is to make sure to include an "ItemsPresenter" object somewhere in the ControlTemplate. It's the place holder for the panel to display the ItemsControl items. An alternative is to include a panel into the ControlTemplate with the property "IsItemsHost" set to 'true'.


33.4 How to edit templates using Microsoft Expression Blend ?

To edit ControlTemplates using Blend do the following steps.

1. Locate the control in the visual tree.

2. Right-click on it and then select,

  • Edit Control Parts (Template) -> Edit a Template / Edit a Copy. Blend would prompt for creating the template if it's not already created.

33.5 How to get the default template of a control programmatically ?

To get the Style for any given WPF control, get the control instance and use the "XAMLWriter" class for writing the XAML related information.

[C#]

System.Windows.Controls.TextBox t = new System.Windows.Controls.TextBox();
StringBuilder sb = new StringBuilder();
using (TextWriter writer = new StringWriter(sb))
{
System.Windows.Markup.XamlWriter.Save(t.Template, writer);
}

bottom user control
::  Home :: Services ::  Prices ::  Request Quote
Copyright 2007, Megasolutions Ltd