2017-05-07 4 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

ボタンテンプレートにコンテンツプレゼンターを追加した後、 コンテンツ値を設定することが可能です。

 <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}" /> 
関連する問題