One solution is to create the IDisposable template itself, and then remove the controls in your Dispose template. Of course, this means that you need some kind of collection to keep track of the controls you created. Here is one way:
public class CustomColumnTemplate : ITemplate, IDisposable { private readonly ICollection<Control> labels = new List<Control>(); public void Dispose() { foreach (Control label in this.labels) label.Dispose(); } public void InstantiateIn(Control container) {
...
Now you are still faced with the problem of deleting the template. But at least your template will be the responsible memory user, because when you call Dispose on the template, all its shortcuts will be placed with it.
UPDATE
This MSDN link suggests that it may not be necessary for your template to implement IDisposable , because the controls will be embedded on the page to manage the tree and be automatically deleted by the wireframe!
source share