views:

557

answers:

4

Hi,

I created a WPF application in Visual Studio 2010 Express (C#) and added the text below to the Application.Resources in App.xaml. I see the style applied to the window in the designer, but when I run the application, the window background is white.

Running in Windows XP on BootCamp on a MacBook Pro if that is a factor.

Thanks in advance,

Christian

    <Style TargetType="{x:Type Window}">
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Offset="0" Color="WhiteSmoke" />
                    <GradientStop Offset="1" Color="Silver" />
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>

        <Setter Property="Padding" Value="20" />
    </Style>
A: 

Stumbled across the following:

http://stackoverflow.com/questions/431940/how-to-set-default-wpf-window-style-in-app-xaml

The answer is that a style will not be applied to derived types.

Christian Pena
I'm not sure that this is the answer. See my post below... looks like it's actually a bug.
JasonD
+1  A: 

i have the same thing happening... works in both the designer and runtime under 3.5 and the beta2 of .net 4, but as soon as i go to the full release of .net 4 and the full release of vs2010 the buttons lose their styles at runtime...

any help please

I apologize, but I am just picking up in Version 4 so I am not sure what conversion issues may be faced when moving from 3.5 to 4.
Christian Pena
A: 

I have the same problem. We developed a library with custom styles for every standart control, it works fine under .net 3.5 both at deigner and at runtime. Today we decided to switch our projects to .net4 (wpf 4 has many new tasty feautures). After that at runtime all styles like this not working at runtime, but applying at designer:

 <Style TargetType={x:Type Button}>
      <Setter Property="FontSize" Value="14"/>
 </Style>

Styles with setted x:Key working fine. But we need to set default style for a theme, not to set it for each control.

raeno
Are you using the standard controls or do you subclass from the standard controls? If you inherit from them then my answer below will give you insight. Otherwise I am not sure as I do not know what is involved in upgrading WPF applications from 3.5 to 4.
Christian Pena
+3  A: 

Microsoft have replicated the problem and it looks like it might be a bug in WPF 4.0.

https://connect.microsoft.com/VisualStudio/feedback/details/555322/global-wpf-styles-are-not-shown-when-using-2-levels-of-references

Following the research done by the person who submitted the bug, I took all of our individual XAML resource files that are included into the merged resource dictionary and cut and paste the style text into a single UberStyles.xaml file. I avoided all use of MergedDictionaries.

This solved the problem and my style information from my WPF 3.5 application came back against my WPF 4.0 application.

To my eye this is a clear bug in WPF 4.0 - I'm not exactly sure how you'd cast this as a feature and the behavior is undocumented. I'm a little concerned of the implications of this for the WPF 4.0 platform as a whole. You would have thought this would have been caught in the testing of the Visual Studio 2010 Release!

Anyway, hope this helps. I've been driven crazy by this bug since we upgraded to VS2010 two weeks ago.

JasonD