WPF Window Style not working at runtime

stylesvisual studio 2010wpf

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>

Best Answer

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.

Related Topic