Wednesday, October 12, 2011

How to get default control template of WPF controls?


All WPF developer might aware about Control Templates in WPF. We can create custom control templates and apply those to controls. By default every WPF control has its own default control template. In this post I will explain how to get default control template from control.

Let’s have a look on below code which writes default template of control in console or output window.

<Window x:Class="WPFTestApplication.DefaultControlTemplate"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Default Control Template" Height="200" Width="200">
    <Grid>
        <ListBox Name="listBox1" />
    </Grid>
</Window>

void DefaultControlTemplate_Loaded(object sender, RoutedEventArgs e)
{
    StringBuilder stringBuilder = new StringBuilder();

    XmlWriterSettings xmlSettings = new XmlWriterSettings();
    xmlSettings.Indent = true;

    using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlSettings))
    {
        XamlWriter.Save(listBox1.Template, xmlWriter);
    }

    Console.WriteLine(stringBuilder.ToString());
}

Output

<?xml version="1.0" encoding="utf-16"?>
<ControlTemplate TargetType="ListBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="1,1,1,1" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True">
    <ScrollViewer Padding="{TemplateBinding Control.Padding}" Focusable="False">
      <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
    </ScrollViewer>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="UIElement.IsEnabled">
      <Setter Property="Panel.Background" TargetName="Bd">
        <Setter.Value>
          <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" />
        </Setter.Value>
      </Setter>
      <Trigger.Value>
        <s:Boolean>False</s:Boolean>
      </Trigger.Value>
    </Trigger>
    <Trigger Property="ItemsControl.IsGrouping">
      <Setter Property="ScrollViewer.CanContentScroll">
        <Setter.Value>
          <s:Boolean>False</s:Boolean>
        </Setter.Value>
      </Setter>
      <Trigger.Value>
        <s:Boolean>True</s:Boolean>
      </Trigger.Value>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

Above example writes the default template of listbox control in console/output window using XML writer. You can use this default template and modify the way you want in your application. You can also save this template in xml file too. Let’s have a look on below code.

void DefaultControlTemplate_Loaded(object sender, RoutedEventArgs e)
{
    XmlWriterSettings xmlSettings = new XmlWriterSettings();
    xmlSettings.Indent = true;

    using (XmlWriter xmlWriter =
     XmlWriter.Create(@"D:\Temp\DefaultTemplate.xml", xmlSettings))
    {
        System.Windows.Markup.XamlWriter.Save(listBox1.Template, xmlWriter);
    }
}

Hope you liked this tip to get default control template programmatically. Please feel free to write feedback/comments in comments section below.

See also –




2 comments:

  1. or you could use Sells' "ShowMeTheTemplate" tool :)

    http://www.sellsbrothers.com/Posts/Details/2091

    ReplyDelete
  2. Yes, That's correct.
    We can also use below tool to see visual tree of control.
    Snoop - http://blois.us/Snoop/
    WPF Inspector - http://wpfinspector.codeplex.com/
    WPF Tree Visualizer - http://wpftreevisualizer.codeplex.com/

    ReplyDelete