Posted by: Zeeshan Amjad | September 6, 2011

PART Control Template and TemplatePartAttribute


We already saw one example of PART Template here. But how do we know if any control has a part template or not? The answer is TemplatePartAttribute. If we open the documentation of any control which has PART Control template then we can see that this attribute is defined for each part. For example in case of progress bar this attribute is define three time for all parts i.e. PART_GlowRect, PART_Indicator and PART_Track.

Similarly if we are going to make our own control that uses the part template, then it is good practice to use this attribute. Although it is not necessary to define this attribute and our control works fine even without it, but it lacks the recommended documentation and might not work property in design mode in Blend. In addition, if someone trying to get the information a bout part template programmatically then nothing appears.

Now we are going to get this information programmatically. The only thing we have to do it get the custom attributes of type TemplatePartAttribute. Here is simple code of it.

Code Snippet
Type type = typeof(ProgressBar);

TemplatePartAttribute[] attr =
    type.GetCustomAttributes(typeof(TemplatePartAttribute), true)
    as TemplatePartAttribute[];

 

We can change it from ProgressBar to any other control such as Calendar or DatePicker etc to see the part template of those controls.

Here is complete C# code of the program.

Code Snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace WpfPartTemplate
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<TemplatePartAttribute> attributes;

        public MainWindow()
        {
            InitializeComponent();

            Type type = typeof(ProgressBar);

            TemplatePartAttribute[] attr =
                type.GetCustomAttributes(typeof(TemplatePartAttribute), true)
                as TemplatePartAttribute[];

            attributes = attr.ToList();

            DataContext = attributes;
        }
    }
}

 

And here is complete XAML code of the program.

Code Snippet
<Window x:Class="WpfPartTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
        Title="PART Template" Height="400" Width="600">
    <Grid>
        <DataGrid Margin="5" ItemsSource="{Binding}"/>
    </Grid>
</Window>

 

This is an output of the program.

TemplatePartAttribute

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: