Reputation: 2442
I'm trying to externalize all my components' style so I can reuse them when needed.
I have a resource named MainMenuItemFontStyle with several setters including a FontSize setter to 14.
When I apply this resource to my FontIcon, the Style gets override by some local parameters. The problem is that I never set these local parameters and I can't find them.
The styles are in a separate file (Styles.xaml) and looks like :
<Style x:Name="FontIconBase" TargetType="FontIcon">
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
</Style>
<Style x:Name="MainMenuItemFontStyle" TargetType="FontIcon" BasedOn="{StaticResource ResourceKey=FontIconBase}">
<Setter Property="FontSize" Value="14"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Foreground" Value="Black"/>
</Style>
I called it simply with a Style parameter :
<FontIcon Grid.Column="0" Glyph="{Binding Icon}" Style="{StaticResource ResourceKey=MainMenuItemFontStyle}"/>
And this is what I get from the properties explorer :
As you can see, my styles are presents but there is some local parameters. And the link to MainContainerView.xaml point to the FontIcon displayed before.
Am I doing it the wrong way ?
Thanks.
Upvotes: 1
Views: 226
Reputation: 34306
I've actually noticed this too, recently.
It appears that the FontIcon
class is setting the value 20 as the FontSize
dependency property's local value, which will take precedence over the style setter. I checked the docs and it didn't mention any specific reason why it is doing so, so I think it may be a bug. Really the default value for a dependency property should be set via the dependency property metadata. Here's a link to a feedback hub post I made if you want to upvote it to their attention.
You can fix this by calling ClearValue(FontSizeProperty)
on the specific FontIcon
instance, or just subclass it and use the subclass in place of FontIcon
:
public class FontIconFixed : FontIcon
{
public FontIconFixed()
{
ClearValue(FontSizeProperty);
}
}
Upvotes: 2
Reputation: 269
I think the MainContainerView.xaml also has the style MainMenuItemFontStyle in which the fontsize is 20. Hence the style you have written on Styles.xaml is being overridden.
Upvotes: 0