在TextBlock上设置默认样式会导致Label和其他控件中的样式也被设置.只有将样式放在Application资源中才会发生这种情况,当我将样式放在Window资源中时,一切都很好.
我还发现VS 2008 Designer和XamlPadX会像您期望的那样显示Label,但只有在现实生活中执行应用程序时才会出现问题.
<Application x:Class="WpfApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="8"/>
</Style>
<Style x:Key="Title" TargetType="Label">
<Setter Property="FontSize" Value="32"/>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
<Window x:Class="WpfApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300"
Title="Window1"
Width="300">
<StackPanel>
<TextBlock Text="TextBlock No Style" Style="{x:Null}"/>
<Label Content="Label No Style" Style="{x:Null}"/>
<TextBlock Text="Default TextBlock"/>
<Label Content="Default Label" Style="{StaticResource Title}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
上面的代码显示:
TextBlock No Style - Default font size (As you would expect)
Label No Style - Size 5 font size (How did this happen?)
Default …Run Code Online (Sandbox Code Playgroud)