XAML路径 - 添加文本

时间:2011-11-09 16:56:00

标签: xaml

我已经制作了以下XAML路径,但希望将“Back”文本添加到其中。

enter image description here

这是我在...中添加它的方式。

   <Path Style="{StaticResource BackButton}">

   </Path>

然而,当我在路径中添加任何内容时,我收到消息

The type 'Path' does not support direct content.

任何提示?

编辑 - 这是样式

   <Style x:Key="BackButton" TargetType="{x:Type Path}">
        <Setter Property="Data" Value="F1 M 639.667,236.467L 645.092,236.467L 725.566,236.467L 725.566,253.513L 645.092,253.513L 639.673,253.513L 619.787,244.99L 639.667,236.467 Z " />
        <Setter Property="Height" Value="30" />
        <Setter Property="MinWidth" Value="100" />
        <Setter Property="Fill" Value="#FFFFFFFF" />
        <Setter Property="StrokeThickness" Value="1" />
        <Setter Property="Stroke" Value="#FF999B9C" />
        <Setter Property="Stretch" Value="Fill" />
        <Setter Property="Cursor" Value="Hand" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Fill" Value="#FFEEEEEE" />
            </Trigger>
        </Style.Triggers>
    </Style>

1 个答案:

答案 0 :(得分:3)

您可以将形状和TextBlock放在Canvas内,并将文字放在形状的顶部。

例如:

<Canvas>
    <Path Style="{StaticResource BackButton}"/>
    <TextBlock Text="Back" Canvas.Top="0" Canvas.Left="25" FontSize="20"/>
 </Canvas>

会给你:

enter image description here

您也可以使用Grid作为容器,并将TextBlock Panel.ZIndex属性更改为更高的值:

 <Grid>           
        <Path Style="{StaticResource BackButton}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5"/>          
        <TextBlock Text="Back" FontSize="20" Panel.ZIndex="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>                    
  </Grid>

有关详细信息,请参阅this tutorial