2016-08-30 58 views
1

我試圖讓按鈕可視化的PNG(無邊框,漸變,無論)。應用基本自定義樣式時隱藏的按鈕 - WPF

我所做的是:

  • 資源

    <Style x:Key="PlainImageButton" TargetType="{x:Type Button}"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="Button"> 
           <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
    </Style> 
    
  • XAML

    <Button Width="16" Height="16" Style="{DynamicResource PlainImageButton}"> 
           <Button.Background> 
            <ImageBrush ImageSource="Images/pencil.png"/> 
           </Button.Background> 
         </Button> 
    

但按鈕是不可見的

enter image description here

爲什麼會發生這種情況?

回答

1

你必須在你的模板中使用提供的背景,例如像這樣:

<Button.Template> 
    <ControlTemplate TargetType="{x:Type ButtonBase}"> 
     <Border x:Name="border" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
      <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" /> 
     </Border> 
    </ControlTemplate> 
</Button.Template> 
0

您正在提供圖像作爲按鈕中的背景。而在您的模板,你還沒有使用的background屬性與ContentPresenter結合

試試這個

<Button Width="16" 
       Height="16" 
       Style="{DynamicResource PlainImageButton}"> 
      <Button.Content> 
       <Image Source="pencil.png" /> 
      </Button.Content> 
     </Button> 

在這裏,我已經設置了內容爲將與ContentPresenter在您的模板被綁定圖像

希望它有幫助:)