InvalidOperationException: can only be based on a style with a target type, which is the base type of TextBlock,

I created the baseStyle style as follows:

<Style TargetType="{x:Type Control}" x:Key="baseStyle">
    <Setter Property="FontSize" Value="30" />
    <Setter Property="FontFamily" Value="Saumil_guj2" />
</Style>

Then I used it for ListBoxItem, for example:

<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource baseStyle}">

</Style>

He gladly accepts FontSizeand FontFamilyfrom baseStyle.

I tried to do a similar thing for TextBlock:

<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource baseStyle}">

</Style>

Now he is complaining. I mean, this gave me the opportunity:

InvalidOperationException: Can only base on a Style with target type 
that is base type 'TextBlock'.

So, I checked the MSDN.

There I discovered that ListBoxItem comes indirectly from System.Windows.Controls. Here can be found here .

There I also found that TextBlock also comes from System.Windows.Controls. You can find it here.

So, I do not understand why I am getting this error?

+4
1

TextBlock, Control, FrameworkElement. TextBlock Control, FontSize FontFamily. . , FrameworkElement, TextElement.FontSize TextElement.FontFamily

<Style TargetType="{x:Type FrameworkElement}" x:Key="baseStyle">
    <Setter Property="TextElement.FontSize" Value="30" />
    <Setter Property="TextElement.FontFamily" Value="Saumil_guj2" />
</Style>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource baseStyle}">

</Style>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource baseStyle}">

</Style>
+12

All Articles