UWP/XAML: How to inherit from the default styles using BasedOn?

只愿长相守 提交于 2019-12-11 04:03:36

问题


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:


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>


来源:https://stackoverflow.com/questions/37336314/uwp-xaml-how-to-inherit-from-the-default-styles-using-basedon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!