Load DataTemplate from resource in code


  • I have a simple question about loading a data template from code.  I have a simple combo box class that derives directly from ComboBox.  eg:


    Code Snippet

    public class SymbolLinkPicker : ComboBox


    // Constructor

    public SymbolLinkPicker()






    What I'd like to do is now, is in the constructor load a data template from generic.xaml.  How do I access that resource dictionary from code?  I've tried this.FindResource but no luck.  Any help appreciated

    Friday, September 21, 2007 1:43 AM


  • Dont worry found it:

    Code Snippet

    ComponentResourceKey templateKey = new ComponentResourceKey(typeof(SymbolLinkPicker), "SymbolLinkDataTemplate");

    this.ItemTemplate = (DataTemplate)this.TryFindResource(templateKey);



    and in generic.xaml;

    Code Snippet

    <DataTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:SymbolLinkPicker}, ResourceId=SymbolLinkDataTemplate}">


    <TextBlock Text="Yikes, that was obvious" />





    Friday, September 21, 2007 4:47 AM