Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #24: How to Apply a XAML Template to a Class

Let’s say you have a class where you want to declare elements via a XAML template instead of dynamically creating them.

For example, the XAML to declare an image and add it to a canvas would look like this:

<Canvas x:Name="MyCanvas">
    <Image x:Name="MyImage" Source="Grass.png"></Image>
</Canvas>

To do the same thing programmatically you would need to declare the following code:

Image myImage = new Image();
Uri uri = new Uri("Grass.png", UriKind.Relative);
ImageSource imgSrc = new System.Windows.Media.Imaging.BitmapImage(uri);
myImage.SetValue(Image.SourceProperty, imgSrc);

The way to do this via a template in a class is to first put the XAML into a string like this:

private const string _contentTemplate
       = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" +
           "                  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
           "<Image x:Name=\"MyImage\" Source=\"Grass.png\"></Image>" +
           "</ControlTemplate>";

Your class will need to inherit from Control:

public class MyControl : Control

Now, in the constructor of your class you can load this template via the XamlReader.Load() method. You will need to add a using statement first to reference the namespace System.Windows.Markup;

public MyControl()
{
    Template = (ControlTemplate)XamlReader.Load(_contentTemplate);
    ApplyTemplate();
}

ApplyTemplate() is where you can get the elements that are in declared in your XAML code. Override the function OnApplyTemplate like this:

private _myImage;
 
public override void OnApplyTemplate()
{
    _myImage = (Image)GetTemplateChild("MyImage");
}

Complete class:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
 
namespace MapEditor
{
    public class MyImage : Control
    {
        Image _myImage = null;
 
        private const string _contentTemplate
               = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" +
                   "                  xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
                   "<Image x:Name=\"MyImage\" Source=\"Grass.png\"></Image>" +
                   "</ControlTemplate>";
 
        public override void OnApplyTemplate()
        {
            _myImage = (Image)GetTemplateChild("MyImage");
        }
 
        public MyImage()
        {
            Template = (ControlTemplate)XamlReader.Load(_contentTemplate);
            ApplyTemplate();
        }
 
    }
}

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

Let’s say you have a class where you want to declare elements via a XAML template instead of dynamically

# August 5, 2008 7:42 PM

Silverlight news for August 6, 2008 said:

Pingback from  Silverlight news for August 6, 2008

# August 6, 2008 11:13 AM

carnewsservice.info » Tip of the Day #24: How to Apply a XAML Template to a Class said:

Pingback from  carnewsservice.info &raquo; Tip of the Day #24: How to Apply a XAML Template to a Class

# August 7, 2008 12:58 PM

Mike Snows Silverlight Blog said:

For this tutorial we will be generating a 2D terrain map. Preview and Run App: http://silverlight.services

# August 8, 2008 6:00 PM

Community Blogs said:

Michael Washington with an add-on for SL Desktop already, Mike Snow beginning Terrain tutorials and applying

# August 10, 2008 9:13 PM

Silverlight news for August 11, 2008 said:

Pingback from  Silverlight news for August 11, 2008

# August 11, 2008 10:27 AM

Visual Web Developer Team Blog said:

7 new Silverlight blogs have been completed. Check them out and let me know if you have any questions.

# August 14, 2008 2:51 PM

codebased said:

Hi there,

I am using two custom controls and got a template in the generic.xaml.

Control 2 is using control 1 and control 1 is setting some public objects in OnApplyTemplate () ... through gettemplateChild

However, the Control 2 OnApplyTemplate does call before OnApplyTemplate for control 2 and through this I cannot have an acces of Control 1 object because they are just null.

Is there any ways to solve this problem?

# October 7, 2008 1:18 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight tips of the

# January 2, 2009 5:56 PM

o UAU nosso de cada dia said:

essa lista eu copiei desse blog bárbaro (acompanhe por RSS você também): uma lista de dicas super úteis

# January 3, 2009 6:25 AM