WPF按钮样式,模板?

时间:2018-05-22 11:07:42

标签: wpf templates button styles

我非常接近得到我想要的东西,但我不喜欢在角落边界之外伸出的背景。任何人都可以指出我正确的方式来设置背景的样式也可以绕过角落吗?感谢。

<Button  FontSize="16" Height="50" >
    <Button.Content>
        <Border CornerRadius="5" Padding="5" Margin="0" BorderBrush="GreenYellow" BorderThickness="2" Height="50" >
            <TextBlock Text="Work Order Manager" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Border>

    </Button.Content>

    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Green"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="DarkGoldenrod"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>

</Button>

1 个答案:

答案 0 :(得分:3)

如果要围绕整个按钮,请将CornerRadius移动到按钮的模板:

<Button  FontSize="16" Height="50" Padding="5" Content="Work Order Manager" >
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Green"/>
            <Setter Property="BorderBrush" Value="GreenYellow"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="{TemplateBinding Background}"
                                CornerRadius="5" 
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}" 
                                >
                            <ContentPresenter 
                                Margin="{TemplateBinding Padding}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="DarkGoldenrod"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>