16

Themes\Generic.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="WPF Commons;component/Controls/Layout/Foo/FooItem.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Controls\Layout\Foo\FooItem.xaml:

<Style TargetType="{x:Type l:FooItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type l:FooItem}">
                <Border>
                    <ContentPresenter ContentSource="Header" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


If I copy the entire style into my usercontrol resources it works fine. But, if I don't, the usercontrol shows up empty. In Expression Blend 4, I right-clicked and chose Edit Template>, but it won't let me select Edit a Copy... which leads me to believe that something is severely wrong and the Generic.xaml isn't loading properly. I figure it's Generic.xaml because if I remove the MergedDictionary call and copy/paste the xaml style directly into Generic.xaml it still doesn't work.

michael
  • 14,844
  • 28
  • 89
  • 177
  • Can you show your code for FooItem? In general, UserControls should not be retemplated (i.e. set Template property) as it doesn't work as expected. – CodeNaked Apr 12 '11 at 17:15
  • FooItem.cs is empty for now, I wanted to make sure I got the layout right and slowly add in other DependencyProperties and logic later. – michael Apr 12 '11 at 17:24
  • What does FooItem derive from? – CodeNaked Apr 12 '11 at 17:30
  • It was because I tried to change the ThemeInfo stuff just like myermian said. Thanks though. – michael Apr 12 '11 at 17:33

2 Answers2

31

I'm gonna take a wild guess that you altered your AssemblyInfo.cs file and either changed (or removed) the following line:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
    //(used if a resource is not found in the page, 
    // or application resource dictionaries)
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries)
)]

You need to tell your assembly about your ThemeInfo. :)

myermian
  • 31,823
  • 24
  • 123
  • 215
  • 3
    Ran into the same problem and fixed using the above attribute. In my case the issue was caused by taking a c# library project that was not originally set up as a WPF Custom Control Library (hence the above attribute was not automatically added to the assembly) and later adding a Custom Control – cordialgerm Mar 09 '12 at 18:32
1

copying from my blog: http://zoomicon.wordpress.com/2012/06/10/what-to-do-if-generic-xaml-doesnt-get-loaded-for-wpf-control/

at the start of Properties\AssemblyInfo.cs you need (note this isn’t used/needed in Silverlight): using System.Windows;

...

Mind you that if the project doesn’t show a Properties node in Solution Explorer, you have to either make a new project using the correct template (for a WPF custom control), or right click the project, select Properties, then press the Assembly Information button and enter some dummy values, then OK to create the Properties node (which also creates to a Properties subfolder and AssemblyInfo.cs file).

You can expand (drop-down) the special Properties node in solution explorer then to open AssemblyInfo.cs and add the above stuff if missing

Community
  • 1
  • 1
  • I also just noticed that if you have a key at the XAML definition of the resource used for the Style of the control, it won't load, you should just use a Target attribute - that probably creates some implicit key internally – George Birbilis Sep 04 '14 at 14:17