2014-08-30 41 views
0

我在app.xaml文件中創建了一個玻璃按鈕(代碼底部)。我有幾個按鈕在mainwindox.xaml中引用此模板。我不知道該怎麼做的是在MainWindow中設置按鈕顯示的文本。下面的代碼的第一部分是其中一個按鈕的示例。某處我需要添加一些代碼,以便按鈕上有文字'相關性'。app.xaml中的WPF按鈕模板如何設置MainWindow.xaml中的文本

下面

<Button Grid.Column="0" Grid.Row="1" Style="{StaticResource buttBasicTemplateReport}" Command="{Binding CommandButtReportsCorrel}" Height="50" Width="120" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0"/>      

的App.xaml代碼主窗口代碼下面

  <!-- Glass Button empty template Report --> 
    <Style x:Key="buttBasicTemplateReport" TargetType="Button"> 
     <Setter Property="Cursor" Value="Hand"/> 
     <Setter Property="FontSize" Value="12" /> 
     <Setter Property="Foreground" Value="White"/> 
     <Setter Property="FontWeight" Value="Bold"/> 
     <Setter Property="VerticalAlignment" Value="Center"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Border x:Name="ButtonBorder" 
           CornerRadius="15,15,15,15" 
           BorderThickness="3,3,3,3" 
           Background="#AA000000" 
           BorderBrush="#99FFFFFF" 
           RenderTransformOrigin="0.5,0.5"> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="*"/> 
           <RowDefinition Height="1.7*"/> 
          </Grid.RowDefinitions> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition/> 
           <ColumnDefinition/> 
          </Grid.ColumnDefinitions> 
          <Border Grid.Row="0" Grid.ColumnSpan="2" CornerRadius="23,23,0,0"> 
           <Border.Background> 
            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> 
             <GradientStop Color="#08FFFFFF" Offset="0"/> 
             <GradientStop Color="#88FFFFFF" Offset="1"/> 
            </LinearGradientBrush> 
           </Border.Background> 
          </Border> 
          <ContentPresenter x:Name="ButtonContentPresenter" 
          VerticalAlignment="Center" 
          Grid.RowSpan="2" 
          HorizontalAlignment="Center"/> 
          <Rectangle x:Name="recGlow" Style="{StaticResource recSecurity}" 
             Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="2"/> 
          <TextBlock Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" Style="{StaticResource txtSecurity}"/> 
         </Grid> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter TargetName="recGlow" Property="Opacity" Value="0.8"/> 
          <Setter Property="RenderTransform" TargetName="ButtonBorder"> 
           <Setter.Value> 
            <TransformGroup> 
             <ScaleTransform ScaleX="1" ScaleY="1"/> 
            </TransformGroup> 
           </Setter.Value> 
          </Setter> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

回答

3

ButtonContentControl。正如我所看到的,您的Template中有一個ContentPresenter。因此,您可以將Content屬性添加到您的Button或添加Contnet元素。

按屬性:

<Button Content="Correlation"/> 

通過元素:

<Button>Correlation</Button> 
+0

當我添加的內容= 「關聯」 按鈕一段顯示 「Correlati」原因是文本不居中。當文本到達按鈕的中間時,文本的其餘部分不可見 – mHelpMe 2014-08-30 16:28:44

+0

由於您已明確設置了「Button」的「Width =」120「',這不足以適應該文本。 – 2014-08-30 16:33:55

+0

當然,你的權利,我現在可以看到所有的文字。但是文本顯示在按鈕的左側,而不是整個按鈕的中央 – mHelpMe 2014-08-30 16:42:12

0

當你做你需要,如果你想的onClick影響使用ContentControl中和觸發器自定義按鈕。 同樣爲了回答你的問題,當你製作自定義按鈕時,你需要將邊距綁定到填充屬性以將按鈕內容居中。

你可以看到這個鏈接:

WPF: Why shouldn't I use {TemplateBinding Margin} in ControlTemplate - is Margin meant only for element's containers?

你可以看到,這也:

http://sshumakov.com/2013/02/19/how-to-bind-to-control-properties-from-controltemplate/