I have just changed my WPF application from .Net3.5 to .Net4. Doing this caused all my global styles to stop working. Only the styles explicitly set using a key did work. I've done some research and figured out what causes this, and reproduced it in a simple app.
I have a simple WPF app containing only a button with text - no style or anything else. I define a style for all buttons in the ResourceDictionary of App.Xaml:
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red"></Setter>
</Style>
My button is now Red - all fine. I now move this to a separate ResourceDictionary in a separate project. This is where I want to hold all my shared styles. The button is still red, and my reference from App.xaml to SharedStyles.xaml looks like this:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/StyleLib;component/SharedStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now - I want SharedStyles.xaml of StyleLib to hold all specific style definitions, so I create a new file in the same project called ButtonStyles.xaml, and I add the resource there. Actually - I add another style too with a key to be used explicitly (technically I added this later, so this does not have anything to do with the problem that occurs).
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Red"></Setter>
</Style>
<Style x:Key="Explicit" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Blue"></Setter>
</Style>
</ResourceDictionary>
ButtonStyles.xaml is referenced from SharedStyles.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/StyleLib;component/ButtonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Now - my button control is not styled any more. Actually - it is still shown as styled in the preview-window in VS2010, but when I run the application they aren't styled. If I explicitly reference the style with key "Explicit" they get this style - so the file is successfully included.
Another funny thing is that if I now add another style in SharedStyle.xaml - e.g. a global style for StackPanel (which was what I tried) - then the global style inside ButtonStyle.xaml magically starts working..!
My question now is if I'm doing something wrong, or if this sounds like a bug in .Net4? Sounds like a bug to me.. This did work just fine in .Net3.5!