WPF Window Style not working at runtime
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>
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.
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.
精彩评论