样式中定义的按钮资源

Button Resources defined in Style

我可以像这样创建一个带有圆角的按钮(来自How to create/make rounded corner buttons in WPF?):

<Button>
    <Button.Resources>
        <Style TargetType="Border">
            <Setter Property="CornerRadius" Value="5"/>
        </Style>
    </Button.Resources>
</Button>

如何在样式中定义它,然后将这种样式应用到每个按钮?我尝试创建这样的样式,但 CornerRadius 不可用:

<Style x:Key = "myButtonStyle" TargetType = "Button">
  <Setter Property = "Height" Value = "30"/>
  <Setter Property = "Width" Value = "80"/>
  <Setter Property = "Margin" Value = "10"/>
</Style>

喜欢这个帕维尔?

<Style x:Key="s2" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"> 
  <Setter Property="Background" Value = "Red" />
  <Setter Property="CornerRadius" Value="5"/>
</Style>

WPF 不允许我

您可以使用嵌套 Border 样式定义隐式按钮样式:

<Style TargetType="Button">
    <Style.Resources>
        <Style TargetType="Border">
            <Setter Property="CornerRadius" Value="5"/>
        </Style>
    </Style.Resources>
    <Setter Property="Height" Value="30"/>
    <Setter Property="Width" Value="80"/>
    <Setter Property="Margin" Value="10"/>
</Style>