2017-05-07 72 views
0

我无法在此特定的模板按钮上显示文本。
我需要内容值出现在这个按钮上。模板按钮上的文本

 <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}" 
    > 
+1

ControlTemplate中没有任何元素会在Content属性中获取和显示值/对象。毫不奇怪,你看不到内容价值......(也许这是一个好主意,看看按钮的标准控制模板,以了解控制模板的工作原理......) – elgonzo

+1

如何让你的手在标准控制模板上(不仅仅是一个按钮):http://stackoverflow.com/a/28212036/2819245。或者使用像StyleSnooper这样的工具:https://github.com/drewnoakes/style-snooper – elgonzo

+0

@elgonzo谢谢你指点我正确的方向。 StyleSnooper似乎很有趣.... – LetzerWille

回答

1

添加内容展示到按钮模板后, 可以设置Content值。

 <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}" />