2017-05-07 69 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}" />