views:

76

answers:

1

I am trying to build a custom control that I want to exist as the root element of my xaml.

I currently have

  1. the generic.xaml template sorted.
  2. A new template in the blend folder that will allow me to make a new 'DaveControl'
  3. The ability in blend to add content to it that will appear in the contentpresenter.

However, If I run it, the content disappears. It stays if I add the control to a usercontrol, but I don't want that to happen. then i won't be able to call base methods since UserControl Inherits from Control and the g.cs will always complain.

I need the class in the codebehind to have an implementation that I can overload a save method on. So the codebehind derives from a class that I wrote.

Effectively, I just want to say New Dave Control, go to the code behind and write one method, 'public override save'

Is this possible?

A: 

Ok, there are a lot of little gotchas to deal with when you try to do this.

If you don't want to run into race conditions and Object not found errors in blend, change the order the template is applied in.

Inside the constructor for your custom class, call

this.ApplyTemplate();

Read this great insight to see why:

http://pagebrooks.com/archive/2008/11/30/tweaking-onapplytemplate-event-timing-in-silverlight-2.aspx

Then, ensure you have the x:Class attribute on your new control. This will mean you get code generated from your xaml, which names all attributes and parses your xaml properly.

DavidA