tags:

views:

370

answers:

3

In a WPF application I defined default control styles in separate resource dictionaries (e.g. "ButtonStyle.xaml"), and added them as merged dictionaries to a resource dictionary named "ResDictionary.xaml".

If I refer this "ResDictionary.xaml" as merged dictionary in my App.xaml, the default styles are not applied. However, if I refer the "ButtonStyle.xaml", it works correctly.

If I recompile the same code in .NET 3.5 or 3.0, it recognizes and applies the default styles referred in "App.xaml" through "ResDictionary.xaml", but not in .NET 4.0.

At runtime if I check the Application.Current.Resources dictionary, the default styles are there, but they are not applied, only if I specify the Style property explicitly in the Button control.

Are there any solutions to refer a resource dictionary (containig default styles) this way in .NET 4.0?


App.xaml:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Styles/ResDictionary.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>

ResDictionary.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Default/ButtonStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

ButtonStyle.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <Style TargetType="Button">
        <Setter Property="Background" Value="Yellow"/>
    </Style>
</ResourceDictionary>
+2  A: 

There is a sort-of fix for this, but I’ve only been able to make it work at the window level (not the application level).

In order to include a WPF 4.0 resource from a separate project, the resource must be added as a resource in the window’s code behind. The statement belongs in the window’s constructor, prior to the InitializeComponent method call:

public ControlsWindow()
{
    this.Resources = Application.LoadComponent(new Uri("[WPF 4.0 ResourceProjectName];Component/[Directory and File Name within project]", UriKind.Relative)) as ResourceDictionary;
    InitializeComponent();
}

Note: Replace the '[WPF 4.0 ResourceProjectName]' text with your resource's project name. Also, the '[Directory and File Name within project]' needs to be replaced with the relative location of the resource file (like 'Themes/StandardTheme.xaml')

I go into more details about this issue here.

I added your code to the App.xaml.cs constructor, and the default styles worked.Thanks!
Burgberger
Yeah, I probably should have mentioned that: the standard (implicit) styles will work when the resource is added to the app.xaml.cs file, but the explicit styles will not... which is strange because it's exactly the opposite of what happens when the resource is added in the XAML file (app.xaml).
Does anyone know of any workarounds other then this? It doesn't seem like the logical way this should work.
Nathan
+2  A: 

The best solution is to add a dummy default style in the resource dictionary where you merge all resources together.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Style/Button.xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style TargetType="Control" BasedOn="{StaticResource {x:Type Control}}" />

Greetings

Christian Moser
www.wpftutorial.net

Christian Moser
No idea how this works but it does!
Stimul8d
A: 

This could be caused by a known bug when there is a single style in application.resources within app.xaml when not using a startupuri.

The fix is to add an additional style like this...

...
   <Style x:Key="unused" />
</Application.Resources>

for more details check out this link.... http://bengribaudo.com/blog/2010/08/19/106/bug-single-application-resources-entry-ignored

Good Luck

Keith
xamtrix.com