WPF Setting default style on TextBlock overrides label style

Setting the default style in the TextBlock causes the style in the shortcut and other controls to be set as well. This only happens if you add styles to the application resources, when I put the style into Window resources, everything is fine.

I also found that VS 2008 Designer and XamlPadX display a shortcut, as you would expect, but the problem only occurs when running the application in real life.

<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> 

The above code displays:

 TextBlock No Style - Default font size (As you would expect) Label No Style - Size 5 font size (How did this happen?) Default TextBlock - Size 5 font size (As expected by my style) Default Label - Size 5 font size (How did this happen?) 
+7
styles wpf default font-size textblock
source share
1 answer

Yes, this can be expected; look at the default template for Label, these are just TextBlock indents. Styles are inherited, so Label will set FontSize to 32, but then the TextBlock style will override this. If you just did this, it would also be 5pt.

Edit: So, how would I solve this, you need to create a dummy subclass (i.e. a class that does not change anything) of a TextBlock called NormalText, and then style; this way, you will not accidentally pick up other text blocks.

+10
source share

All Articles