2012-04-10 126 views
0

我是新來的,所以請耐心等待。我現在有Style。將其粘貼到一個窗口中,以便在操作中看到它。WPF按鈕樣式不顯示文本

<Window.Resources> 

<Style x:Key="SelectionButton3" 
     TargetType="{x:Type Button}"> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 

       <Grid HorizontalAlignment="Stretch" 
         VerticalAlignment="Stretch" 
         ClipToBounds="False"> 

        <Grid.RowDefinitions> 
         <RowDefinition Height="75" /> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="100" /> 
         <ColumnDefinition Width="55" /> 
        </Grid.ColumnDefinitions> 

        <Border x:Name="TheBorder" 
          BorderThickness="0,1.5,1.5,1.5" 
          CornerRadius="3" 
          Background="SteelBlue" 
          Height="35" 
          Grid.Column="1" 
          Grid.Row="0" 
          Margin="-31" 
          BorderBrush="DarkSlateBlue"/> 

        <Rectangle Name="TheRectangle" 
           Fill="SteelBlue" 
           Stroke="DarkSlateBlue" 
           Grid.Row="0" 
           Grid.Column="0"> 

         <Rectangle.LayoutTransform> 
          <RotateTransform Angle="-45" /> 
         </Rectangle.LayoutTransform> 

         <Rectangle.BitmapEffect> 
          <DropShadowBitmapEffect ShadowDepth="5" /> 
         </Rectangle.BitmapEffect> 

        </Rectangle> 

        <ContentPresenter x:Name="ContentArea" 
             VerticalAlignment="Center" 
             HorizontalAlignment="Center" 
             Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" /> 
       </Grid> 

       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" 
           Value="True"> 
         <Setter TargetName="TheBorder" 
           Property="BitmapEffect"> 
          <Setter.Value> 
           <DropShadowBitmapEffect ShadowDepth="0" /> 
          </Setter.Value> 
         </Setter> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 

    </Setter> 

    <Setter Property="Foreground" 
      Value="White"/> 
    <Setter Property="FontFamily" 
      Value="Segoe UI" /> 
    <Setter Property="FontSize" 
      Value="20" /> 

    </Style> 

<Grid.RowDefinitions> 
    <RowDefinition Height="20"/> 
    <RowDefinition Height="Auto"/> 
    <RowDefinition Height="Auto" /> 
    <RowDefinition Height="Auto" /> 
    <RowDefinition Height="Auto" /> 
</Grid.RowDefinitions> 
<Grid.ColumnDefinitions> 
    <ColumnDefinition Width="20"/> 
    <ColumnDefinition Width="250" /> 
</Grid.ColumnDefinitions> 

<Button Grid.Row="1" 
     Grid.Column="1" 
     Style="{StaticResource SelectionButton3}" 
     Margin="0,0,0,5" 
     Click="Button_Click"> 

    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="Auto" /> 
     </Grid.ColumnDefinitions> 

     <Image Height="35" 
       Width="35" 
       Stretch="Fill" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center" 
       Grid.Column="0" 
       Margin="32.5,0,0,0" 
       Source="/app;component/Media/Images/Mail/email.png" /> 

     <TextBlock Text="Email" 
        Grid.Column="1" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" 
        Margin="22,0,0,0"/> 

    </Grid> 


</Button> 

我有2個問題:

1)爲什麼只有部分文字顯示出來?

2)如何將ImageTextBlock置於Template中,並將其綁定到它們上?

回答

2

您的文本被剪切的原因是因爲您沒有爲您的ContentPresenter指定Grid.Row和Grid.Column。這會導致按鈕的內容默認爲ControlTemplate上的第0行和第0列,並與您的邊距和位置一起使您的按鈕元素上的文本塊被切斷。我猜這可能是你的意圖?

<ContentPresenter x:Name="ContentArea" 
    Grid.Column="0" 
    Grid.ColumnSpan="2" 
    Grid.Row="0" 
    VerticalAlignment="Stretch" 
    HorizontalAlignment="Stretch" 
    Content="{Binding Path=Content, 
      RelativeSource={RelativeSourceTemplatedParent}}" /> 

現在ContentPresenter將跨越上ControlTemplate的電網兩列,讓你的文字塊將有更多的空間來展示自己。

要回答你的第二個問題做How do I put the Image and Textblock in the template and just bind to them?

一種方式,它是繼承Button類,稱之爲MyImageButton例如,實施兩個新的依賴屬性在這個新類叫ImageText。然後,您可以將您的<Image><TextBlock>放入您的控制模板中,並將它們綁定到新的ImageText屬性。然後,您可以實例化MyImageButton並使用新的屬性來設置按鈕的圖像和文本。

+0

就是這樣。謝謝 – CoderForHire 2012-04-10 23:15:47