views:

36

answers:

1

I'm trying to learn Silverlight here, creating a custom control template, however VS2010 refuses to recognize the ControlTemplate type in markup code, even though I have referenced the System.Windows and System.Windows.Controls assemblies (which is by default when basing the project on the standard Silverlight Application template). I'm trying to recreate this seen on another SO stack.

I've tried putting this code directly into a file (i.e. ImageButton.xaml) and nothing else:

<ControlTemplate x:Key="ImageButtonTemplate">
    <Image Source="{TemplateBinding Content}" />
</ControlTemplate>
A: 

It's a bit hard to answer this question authoritatively without knowing a little more context, such as what type of file you are placing this in, and what the exact error is from Visual Studio. I imagine that you're getting an error such as:

The type 'ControlTemplate' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

or possibly:

Property 'Content' does not support values of type 'ControlTemplate'

These are caused by placing the template in the wrong place - for example, if you create a new UserControl (via Add -> New Item) and delete the contents of the file and paste in your code, then you will get this error, since the xaml has no references to ControlTemplate.

The best place to put your ControlTemplate is somewhere reusable, such as a new "Resource Dictionary" (again, add it via Add -> New Item -> Silverlight Resource Dictionary) and then place your code inside the <ResourceDictionary ...></ResourceDictionary> tags.

If you want to place it within a UserControl (the source of the second error) then you should add it to the Resources section of that control, for example:

<UserControl.Resources>
    <ControlTemplate x:Key="ImageButtonTemplate">
        <Image Source="{TemplateBinding Content}" />
    </ControlTemplate>
</UserControl.Resources>
Oren