简体   繁体   English

WinUI UWP - 如何创建继承自它的自定义样式?

[英]WinUI UWP - How to create a custom style that inherits from it?

I have added the nuget package for WinUI and added the merged dictionary in App.xaml and new style does appear for all controls that are not affected by the Style directives.我已经为 WinUI 添加了 nuget package 并在 App.xaml 中添加了合并字典,并且对于不受Style指令影响的所有控件都会出现新样式。 However simple using of Style causes the Setters of Style to be applied to the original UWP templates and not to WinUI templates.然而,简单地使用Style会导致将Style设置器应用于原始 UWP 模板而不是 WinUI 模板。 For example this code:例如这段代码:

    <Grid.Resources>
        <Style TargetType="ComboBox">
            <Setter Property="Width" Value="160"/>
        </Style>
    </Grid.Resources>

will cause that ComboBoxes appear without corner radius and with more thick borders than if the above is not applied.将导致 ComboBoxes 出现没有圆角半径并且比不应用上述内容时具有更厚的边框。

Any solution for this?有什么解决办法吗?

To override properties on the WinUI style, you can use based-on styles .要覆盖 WinUI 样式的属性,您可以使用基于 styles

So in your specific case, you can do the following:因此,在您的具体情况下,您可以执行以下操作:

<Style TargetType="ComboBox" BasedOn="{StaticResource DefaultComboBoxStyle}">
    <Setter Property="Width" Value="160"/>
</Style>

That way, you create a new style which is based on the WinUI style (called DefaultComboBoxStyle) where you override the width property.这样,您将创建一个基于 WinUI 样式(称为 DefaultComboBoxStyle)的新样式,您可以在其中覆盖 width 属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM