在样式中定义的按钮资源

5

我可以创建一个圆角按钮,就像这样(来自如何在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>

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

WPF不允许我做


1
尝试添加 BasedOn="{StaticResource {x:Type Button}}" - Pavel Anikhouski
1个回答

4

您可以使用嵌套的 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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接