【问题标题】:Modify a custom styled button's border in WPF XAML在 WPF XAML 中修改自定义样式按钮的边框
【发布时间】:2019-11-07 01:22:26
【问题描述】:

我正在尝试通过 XAML 代码实现this,但无济于事。

我有以下 XAML 代码:

App.xaml

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="Black"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Path Fill="{TemplateBinding Background}"
                          Data="M 0,22.5 22.5,0 27.5,5 10,22.5 27.5,40 22.5,45"/>
                    <Border x:Name="border"
                            BorderThickness="2"
                            BorderBrush="Red"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后像这样使用它:

<Button Style="{StaticResource ButtonStyle}" Height="100" Width="100"/>

它给了我this,但我希望红色边框位于按钮内。

我尝试添加这个

<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="2"/>

而不是创建一个网格,但现在不显示边框。

我想我可以创建另一条路径作为边框,但有没有更简单的方法?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:
    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Black"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Path Fill="{TemplateBinding Background}" Stroke="Red" StrokeThickness="3"
                          Data="M 0,22.5 22.5,0 27.5,5 10,22.5 27.5,40 22.5,45"/>
                        <Border x:Name="border"
                            BorderThickness="2"
                            BorderBrush="Red"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    尝试使用 Stroke 和 StrokeThickness 属性。

    【讨论】:

    • 谢谢,它成功了。由于某些原因&lt;Path Fill="{TemplateBinding Background}" Stroke="RosyBrown" StrokeThickness="2" Data="M 0,22.5 22.5,0 27.5,5 10,22.5 27.5,40 22.5,45"/&gt; 产生了this,没有底线。我必须将其更改为 &lt;Polygon Points="0,22.5 22.5,0 27.5,5 10,22.5 27.5,40 22.5,45" Fill="Black" Stroke="RosyBrown" StrokeThickness="2"/&gt; 以获得所需的结果。
    猜你喜欢
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2015-02-04
    • 1970-01-01
    相关资源
    最近更新 更多