Window style not visible in designer

1.4k views Asked by At

I created a Window style (WPF) and added it as a dll to my project this style shows corretly when i run the program but doesn't show up in the designer.

I googled already but none of the solutions there are working

Test 1:

// Window //
Style="{DynamicResource HVE_Window}"

// Window.Resources //
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/GlobalHive.Styles;component/HiveWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>

Result:

Error: 'Window' TargetType doesn not match type of element 'WindowInstance'

-> But it runs and display correctly there

Test 2:

// Window //
Style="{DynamicResource MyWindow}"

// Window.Resources //
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/GlobalHive.Styles;component/HiveWindow.xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style x:Key="MyWindow" TargetType="{x:Type Window}" BasedOn="{StaticResource HVE_Window}" />

Result:

No Error:
Still doesn't display in the designer, still shows up if i run the program

Test 3:

Both versions but added to application resources

How it should look: running design

How it looks inside the designer: designer design

1

There are 1 answers

2
Andy On

You can sometimes find that resources from a control library are not loaded at design time, despite whatever you put in app.xaml to try and load the things.

MS created a mechanism for Blend which you can use in visual studio since it's the blend designer.

This uses a "special" resource dictionary called DesignTimeResources.xaml

This will only be used at design time.

Add one to the Properties of your problem exe project.

With exactly that name.

Put all your merges into that.

eg this is one of mine from my MapEditor project that uses numerous resources from UILib. UILib is a control library with all sorts of UI stuff in it.

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:MapEditor">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary  Source="pack://application:,,,/UILib;component/Resources/Geometries.xaml"/>
            <ResourceDictionary  Source="pack://application:,,,/UILib;component/Resources/ControlTemplates.xaml"/>
            <ResourceDictionary  Source="pack://application:,,,/UILib;component/Resources/FontResources.xaml"/>
            <ResourceDictionary  Source="pack://application:,,,/UILib;component/Resources/UILibResources.xaml"/>
            <ResourceDictionary  Source="/Views/Drawing/Terrain/Resources/CityResources.xaml"/>
            <ResourceDictionary  Source="/Resources/MapEditorResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

Unload your csproj ( right click in solution explorer), edit it and find the node for that resource dictionary.

Change it to:

<Page Include="Properties\DesignTimeResources.xaml">
  <SubType>Designer</SubType>
  <Generator>MSBuild:Compile</Generator>
  <ContainsDesignTimeResources>true</ContainsDesignTimeResources>
</Page>

Reload the project, close and re-open visual studio.

Your styles should now apply.