WPF 中的自定义形状按钮不会显示文本块
Custom shaped button in WPF won't display text block
我正在尝试创建一个具有自定义形状的按钮,我通过 Path
定义了它,它工作正常,但我无法制作任何按钮 Content
来显示。我可以看到正确形状的按钮和看似正确的行为,但 TextBlock
不会出现,我不明白为什么。
这是一个简单的例子,说明我是如何尝试做到这一点的。我怎样才能让文字真正出现?
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button" x:Key="TestButton">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Path Data="M0.5,0.5 L99.500001,0.5 79.774476,97.648209 78.879563,97.219429 C70.748094,93.55407 61.630468,91.5 52.000001,91.5 41.299485,91.5 31.232103,94.035889 22.447118,98.50029 L22.394687,98.528488 z" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="99.028" Stretch="Fill" VerticalAlignment="Top" Width="100">
<Path.Style>
<Style TargetType="{x:Type Path}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Stroke" Value="LightGray" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
<ControlTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center" />
</Style>
</ControlTemplate.Resources>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource ResourceKey=TestButton}" Margin="154,80,-154,-80" >
<TextBlock>Test</TextBlock>
</Button>
</Grid>
</Window>
您需要放置在您的控件模板中,以便可以显示 TextBlock(content):
<ControlTemplate TargetType="Button">
<Grid>
<ContentPresenter/>
<Path ....../>
</Grid>
您可以随意重新排列路径和内容。
我正在尝试创建一个具有自定义形状的按钮,我通过 Path
定义了它,它工作正常,但我无法制作任何按钮 Content
来显示。我可以看到正确形状的按钮和看似正确的行为,但 TextBlock
不会出现,我不明白为什么。
这是一个简单的例子,说明我是如何尝试做到这一点的。我怎样才能让文字真正出现?
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1" x:Class="WpfApplication1.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button" x:Key="TestButton">
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Path Data="M0.5,0.5 L99.500001,0.5 79.774476,97.648209 78.879563,97.219429 C70.748094,93.55407 61.630468,91.5 52.000001,91.5 41.299485,91.5 31.232103,94.035889 22.447118,98.50029 L22.394687,98.528488 z" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="99.028" Stretch="Fill" VerticalAlignment="Top" Width="100">
<Path.Style>
<Style TargetType="{x:Type Path}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Stroke" Value="LightGray" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
<ControlTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center" />
</Style>
</ControlTemplate.Resources>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Style="{StaticResource ResourceKey=TestButton}" Margin="154,80,-154,-80" >
<TextBlock>Test</TextBlock>
</Button>
</Grid>
</Window>
您需要放置在您的控件模板中,以便可以显示 TextBlock(content):
<ControlTemplate TargetType="Button">
<Grid>
<ContentPresenter/>
<Path ....../>
</Grid>
您可以随意重新排列路径和内容。