c# - WPF XAML - 创建一个带有边框、路径和文本相互叠加的按钮,所有样式都由按钮控制

标签 c# wpf vb.net xaml

我正在尝试创建一个 Button,它有一个 Border(圆角),它有一个 Path 向量覆盖它和Path 上面有一些来自 Label 的文本。

我创建了触发器,当您将鼠标悬停在该 Button 上时,所有这些嵌套元素的样式都会发生变化。

问题 我不确定在哪里放置标签文本,因为我仍然需要从后面的代码访问它以获取和设置它的文本值,但我还需要触发或将其颜色绑定(bind)到我无法访问的边框颜色从 Template 内部或外部来实现这一切。

也许我需要使用不同的布局结构?尽管到目前为止,除了 Label

大部分都工作正常

xaml 文件中的代码

<Grid>
    <Button x:Name="btn" Width="70" Height="70" Background="#FF474747">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border1" BorderBrush="Gray" BorderThickness="3" CornerRadius="2" Background="#FF272727">
                    <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="pth"  Opacity="1" Fill="#FF515151" Stretch="Fill" HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Path.Data>
                            <PathGeometry Figures="m 18.16782 0 -7.64541 5.81323 0 61.04669 L 0 66.85992 0 70 l 61.43133 0 0 -3.14008 -20.17455 0 0 -55.87557 L 18.16782 0 Z m -4.15996 15.70413 23.54902 0 0 1.69254 -20.74683 1.78854 0 15.00717 20.74683 1.78792 0 1.69254 -23.54902 0 0 -21.96871 z m 11.2761 5.08198 a 5.793155 5.793155 0 0 1 5.79266 5.79266 5.793155 5.793155 0 0 1 -5.79266 5.79328 5.793155 5.793155 0 0 1 -5.79328 -5.79328 5.793155 5.793155 0 0 1 5.79328 -5.79266 z" FillRule="evenodd"/>
                        </Path.Data>
                    </Path>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="border1" Property="BorderBrush"  Value="#FFDE2029"/>
                        <Setter TargetName="pth" Property="Fill" Value="#FFAAAAAA"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="border1" Property="BorderBrush"  Value="#FFFF445D"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>

    <Label x:Name="lblText" Content="Ab" HorizontalAlignment="Right" VerticalAlignment="Top" FontFamily="Arial"  Foreground="{Binding BorderBrush, ElementName=btn}" FontSize="22" />
</Grid>

最佳答案

使用 Grid 并将 Lable 和 Path 放入其中并更改 Binding:

 <Window x:Class="Test1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Test1"
    mc:Ignorable="d"
    Title="MainWindow" Name="window"
    xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">

<Grid>
    <Button x:Name="btn" Tag="ABC" Width="70" Height="70" Background="#FF474747">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border1" BorderBrush="Red" BorderThickness="3" CornerRadius="2" Background="#FF272727">
                    <Grid>
                        <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="pth"  Opacity="1" Fill="#FF515151" Stretch="Fill" HorizontalAlignment="Center" VerticalAlignment="Center">
                            <Path.Data>
                                <PathGeometry Figures="m 18.16782 0 -7.64541 5.81323 0 61.04669 L 0 66.85992 0 70 l 61.43133 0 0 -3.14008 -20.17455 0 0 -55.87557 L 18.16782 0 Z m -4.15996 15.70413 23.54902 0 0 1.69254 -20.74683 1.78854 0 15.00717 20.74683 1.78792 0 1.69254 -23.54902 0 0 -21.96871 z m 11.2761 5.08198 a 5.793155 5.793155 0 0 1 5.79266 5.79266 5.793155 5.793155 0 0 1 -5.79266 5.79328 5.793155 5.793155 0 0 1 -5.79328 -5.79328 5.793155 5.793155 0 0 1 5.79328 -5.79266 z" FillRule="evenodd"/>
                            </Path.Data>
                        </Path>
                        <Label x:Name="lblText" HorizontalAlignment="Right" VerticalAlignment="Top" FontFamily="Arial"  
                               Content="{Binding Path=Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Border}}"
                               Foreground="{Binding Path=BorderBrush, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Border}}" FontSize="22" />
                    </Grid>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="border1" Property="BorderBrush"  Value="#FFDE2029"/>
                        <Setter TargetName="pth" Property="Fill" Value="#FFAAAAAA"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="border1" Property="BorderBrush"  Value="#FFFF445D"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>
</Window>

public MainWindow()
{
     InitializeComponent();
        Tag = "ABC";
}

请注意,您可以以类似的模式绑定(bind)内容属性。

关于c# - WPF XAML - 创建一个带有边框、路径和文本相互叠加的按钮,所有样式都由按钮控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42229737/

相关文章:

c# - Sonarqube for TFS 构建时响应错误 500

c# - 通过 ItextSharp 合并 PDF

c# - 如何使用 open xml sdk 从 c# 更改页面方向

vb.net - 找出最大的质数

c# - 传递 HTML 页面字符串并使用 HtmlAgilityPack 进行抓取

c# - 如何使用方法调用作为参数

c# - 带有可选 canExecute 的异步 ICommand 实现

.net - WPF ComboBox 的自定义相等比较器

c# - WPF RowEditEnding 返回旧值

c# - 是否可以在 asp.net[c#] 项目中添加一些 VB 类?