UWP/XAML: How to inherit from the default styles u

2019-07-19 22:14发布

The official Microsoft article states:

Modify the default system styles

You should use the styles that come from the Windows Runtime default XAML resources when you can. When you have to define your own styles, try to base your styles on the default ones when possible (using based-on styles as explained earlier, or start by editing a copy of the original default style).

I understand that you can copy and paste the default style from MSDN in order to "start by editing a copy of the original". However, that strikes me as very ugly and inelegant, as I'm pasting in almost a 100 lines even if I just want to add one thing.

I like the idea of "using based-on styles" to include all of the default style by reference, but from what I can tell the original default styles supplied by Microsoft are implicit. Given that they have no key to reference them by, how can BasedOn be used?

1条回答
劫难
2楼-- · 2019-07-19 22:39

You are right about that BasedOn won't work with default styles as they are implicit.

However, you don't have to include the full style code if you simply want to edit some properties.

For example, the Button below will inherit everything from the default style except the Background color being changed to Red.

<Page.Resources>
    <Style x:Key="RedButtonStyle"
           TargetType="Button">
        <Setter Property="Background"
                Value="Red" />
    </Style>
</Page.Resources>

<Grid>
    <Button Content="Red" Style="{StaticResource RedButtonStyle}" />
</Grid>
查看更多
登录 后发表回答