hexadecimate

What's a wedding? Webster's dictionary describes it as the act of removing weeds from one's garden.

Silverlight 2 – Using Generic.xaml to define control templates

I’ve seen a number of posts describing troubles in getting Generic.xaml working in a Silverlight 2 project.  After going through some of the common pitfalls myself, I thought it might be useful to post a concise list of steps required.

  1. In Silverlight 2 and beyond, place the Generic.xaml file in a (project root) folder named Themes.  (Names are not case-sensitive)
    generic-path
  2. Make sure Generic.xaml is configured such that:
    • Build Action is set to "Resource"
    • Copy to Output Directory is set to "Do not copy"
    • Custom Tool is blank
    generic-properties
  3. In any of your controls that are trying to use templates defined in Generic.xaml, make sure you set the DefaultStyleKey property in your class constructor.
    			public MyControl(){    this.DefaultStyleKey = typeof(MyControl);}		
    	
  4. Verify the general structure of your generic.xaml file.
  5. 			<ResourceDictionary     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:my="clr-namespace:MyExampleControl">    <Style TargetType="my:MyControl">        <Setter Property="Template">            <Setter.Value>                <ControlTemplate TargetType="my:MyControl">                    <TextBlock Text="Foo" />                </ControlTemplate>            </Setter.Value>        </Setter>    </Style></ResourceDictionary>		
    	

Remember that control templates are only applied when the control is added to the visual tree.  If you're still not able to see your template after verifying the above steps, take a close look at your code to make sure your control is being added to the visual tree for rendering.

Posted: Nov 16 2008, 14:48 by warren | Comments (1) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Silverlight

Comments

Add comment


 

biuquote
  • Comment
  • Preview
Loading