【问题标题】:How to style nested controls in UWP with a single style?如何在 UWP 中使用单一样式设置嵌套控件的样式?
【发布时间】:2020-12-30 08:05:03
【问题描述】:

在一个 UWP 项目中,我在一个按钮内的边框内有一个 TextBlock。像这样:

<Button Style="{StaticResource MyBtnStyle}">
   <Border>
     <TextBlock/>
   </Border>
</Button>

我想设置一种同时自定义所有控件的样式。像这样的:

<Style x:Name="MyBtnStyle" TargetType="Button">
   <Setter Property="Background" Value="Gray"/>
   <Style TargetType="Border>
   <Setter Property="BorderThickness" Value="0,0,0,5"/>
       <Style TargetType="TextBlock">
       <Setter Property="FontSize" Value="20"/>
       </Style>
   </Style>  
</Style>

有没有办法做到这一点?如果不是,最好的方法是什么?

【问题讨论】:

    标签: xaml uwp styles


    【解决方案1】:

    不,你不能那样做。您可以为 Button 创建一个样式并更改它的模板属性。然后,您可以自定义边框和文本块,以替换默认按钮样式中的默认 ContentPresenter。

    另一种方法是为每个部分创建三个单独的样式并分别使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-21
      • 2016-12-13
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2020-06-28
      • 1970-01-01
      相关资源
      最近更新 更多