C# 模板按钮上的文本

C# 模板按钮上的文本,c#,wpf,xaml,C#,Wpf,Xaml,我无法使此特定模板按钮上的文本可见。 我需要内容值显示在此按钮上 <ControlTemplate x:Key="BooksButton" TargetType="{x:Type Button}"> <Grid Margin="0,9,0,0"> <Rectangle x:Name="rctBooks"

我无法使此特定模板按钮上的文本可见。 我需要内容值显示在此按钮上

     <ControlTemplate x:Key="BooksButton" TargetType="{x:Type Button}">
                <Grid Margin="0,9,0,0">
                    <Rectangle
                        x:Name="rctBooks"
                        Width="97"
                        Height="40"
                        Margin="-2,0,-1,-4"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        RadiusX="10"
                        RadiusY="10"
                        Stroke="AliceBlue"
                        StrokeThickness="2">
                        <Rectangle.Fill>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Offset="0" Color="Black" />
                                <GradientStop Color="#FFCBC6A9" />
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                </Grid>
            </ControlTemplate>

   <Button
        x:Name="btnMarkTwainStories"
        Width="96"
        Height="50"
        Margin="10,0,0,0"
        HorizontalAlignment="Left"
        Click="btnMarkTwainStories_Click"
        Content="Mark Twain"
        FontFamily="Times"
        FontSize="24"
        Foreground="Black"
        Template="{DynamicResource BooksButton}" 
   >

将内容演示者添加到按钮模板后, 可以设置内容值

      <ControlTemplate x:Key="BooksButton" TargetType="{x:Type Button}">
            <Grid Margin="0,9,0,0">
                <Rectangle
                    x:Name="rctBooks"
                    Width="97"
                    Height="40"
                    Margin="-2,0,-1,-4"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Top"
                    RadiusX="10"
                    RadiusY="10"
                    Stroke="AliceBlue"
                    StrokeThickness="2">
                    <Rectangle.Fill>
                        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                            <GradientStop Offset="0" Color="Black" />
                            <GradientStop Color="#FFCBC6A9" />
                        </LinearGradientBrush>
                    </Rectangle.Fill>
                </Rectangle>
                <ContentPresenter
                    x:Name="contentPresenter"
                    Margin="{TemplateBinding Padding}"
                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                    Content="{TemplateBinding Content}"
                    ContentStringFormat="{TemplateBinding ContentStringFormat}"
                    ContentTemplate="{TemplateBinding ContentTemplate}"
                    Focusable="False"
                    RecognizesAccessKey="True"
                    SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
            </Grid>
        </ControlTemplate>

 <Button
                            x:Name="btnMarkTwainStories"
                            Width="96"
                            Height="50"
                            Margin="10,0,0,0"
                            HorizontalAlignment="Left"
                            Click="btnMarkTwainStories_Click"
                            Content="Mark Twain"
                            FontFamily="Times"
                            FontSize="14"
                            Foreground="Black"
                            FontWeight="Bold"
                            Template="{DynamicResource BooksButton}" />

您的ControlTemplate中没有任何元素可以获取并呈现Content属性中的值/对象。难怪你看不到内容的价值。。。也许最好看看按钮的标准控件模板,了解控件模板是如何工作的……如何使用标准控件模板而不仅仅是按钮:。或者使用StyleSnooper这样的工具:@elgonzo谢谢你为我指明了正确的方向。StyleSnooper似乎很感兴趣。。。。