Search

DefaultStyleKey style not found in inner MergedDictionaries by Andrej Burger

Active

17
1
Sign in
to vote
Type: Bug
ID: 553528
Opened: 4/23/2010 8:13:04 AM
Access Restriction: Public
1
Workaround(s)
11
User(s) can reproduce this bug
Situation:
Window.Resources.MergedDictionaries contains AdministrationResources.xaml (ResourceDictionary)
AdministrationResources.xaml contains GlobalResources.xaml in MergedDictionaries

GlobalResource.xaml contains style:
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
    ...
</Style>

Window contains control:
<Button x:Name="OKButton" Content="OK" />

What I found out:
Style in GlobalResources is applied in VS designer, but not in runtime (default WPF button style is used).
If style is defined in Window.xaml or in AdministrationResources.xaml it works good.
If I set buttons style explicitly it works good. Even if I set it to the value of DefaultStyleKey.
<Button x:Name="OKButton" Content="OK" Style="{StaticResource {x:Type Button}}" />

Default style key is always set correctly to typeof(Button).
Window.FindResource(typeof(Button)) works correctly.

It is working code from .NET 3.5 SP1, now it does not work in .NET 4.0 supplied with VS2010 RTM.

I think:
When looking for style defined by DefaultStyleKey (not set explicitly), only one level of merged dictionaries is searched.
Details (expand)

Product Language

English

Visual Studio Version

.NET Framework 4

Operating System

Windows Vista

Operating System Language

English

Steps to Reproduce

See description

Actual Results

Style defined by DefaultStyleKey is not used

Expected Results

It should be used in any level of merged dictionaries
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey.

 

File Attachments
File Name Submitted By Submitted On File Size  
DictionarySample.zip 4/25/2010 17 KB
Sign in to post a comment.
Posted by pkupis on 9/2/2011 at 7:03 AM
This workaround ensures that merged dictionaries from next level are searched, but they don't see each other (dictionary being loaded cannot access resources defined in dictionaries loaded before). See example to bug 686900 (https://connect.microsoft.com/WPF/feedback/details/686900/using-type-from-external-assembly-in-styles-basedon-causes-error#details). In that example it causes that dict2.xaml has to be merged again inside dict3.xaml.
Posted by Microsoft on 6/6/2010 at 10:08 AM
This is being tracked by an internal bug. Thanks for reporting.
Posted by Andrej Burger on 5/25/2010 at 4:59 AM
Adding <Style TargetType={x:Dummy} /> to Level1.xaml resource dictionary helps. Thanks a lot for workaround.
Posted by Microsoft on 5/24/2010 at 1:48 PM
I believe this is a duplicate of a bug already in out database.
    
On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied.
As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible.
One of them is that we don’t look inside Resource Dictionaries unless they are flagged as “containing default Styles”.
There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it.
The work around is to put a default Style to something, anything, <Style TargetType={x:Dummy} /> in the root Dictionary.
Posted by Microsoft on 4/27/2010 at 3:11 AM
We were able to reproduce the issue described by the customer. We also emailed the customer telling him/her that we were able to reproduce the issue and that we routed the issue to the product unit.
Posted by Andrej Burger on 4/26/2010 at 12:09 AM
Sample project attached. After running application, you can see one small button (style not applied) and one big button (style applied). If you move the style to Level1.xaml (or Window1.xaml) it works on both buttons. Be aware that everything is good in VS designer. It does not work in RUNTIME.

.Net version: 4.0.30319
Posted by Microsoft on 4/25/2010 at 10:56 PM

Thanks for reporting the issue.
In order to fix the issue, we must first reproduce the issue in our labs. We are unable to reproduce the issue with the steps you provided.

Could you please attach a zipped project file to this feedback through our site to help us reproduce the issue?

Thanks again for your efforts and we look forward to hearing from you.
Visual Studio Product Team
Sign in to post a workaround.
Posted by Andrej Burger on 5/25/2010 at 4:58 AM
Add <Style x:Key="{x:Type FrameworkElement}" /> to AdministrationResources.xaml (or Level1.xaml in attached sample).