2015-04-02 94 views
0

我想學習定製WPF ButtonsWPF按鈕模板

早些時候,我使用ButtonContent圖像,但我想將其更改爲基於XAML的對象。

我正在嘗試將Path安裝到ButtonContent中,但無法正確顯示。

我的版本是低於

<Window.Resources> 
    <Style x:Key="MyStyle" TargetType="{x:Type Button}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Grid> 
         <Rectangle RadiusY="2" RadiusX="2" Stroke="Black" Fill="Gray" /> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="0,0,0,0" Visibility="Visible"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="Height" Value="22" /> 
     <Setter Property="Width" Value="22" /> 
    </Style> 
</Window.Resources> 

<Button VerticalAlignment="Center" HorizontalAlignment="Center" Style="{DynamicResource MyStyle}"> 
    <Path Fill="Black" RenderTransformOrigin="0.375,0.375" > 
     <Path.RenderTransform> 
      <ScaleTransform ScaleX="0.375" ScaleY="0.375" CenterX="0" CenterY="0"/> 
     </Path.RenderTransform> 
     <Path.Data> 
      <PathGeometry Figures="M22,18L22,8 18,8 18,18 8,18 8,22 18,22 18,32 22,32 22,22 32,22 32,18z" /> 
     </Path.Data> 
    </Path> 
</Button> 

,但它並沒有結束漲得很好,我不能讓ScaleTransform正常工作:

enter image description here

我試圖讓這個種類結果:

enter image description here

回答

1

不知道,那transorm - 是你所需要的,如果你只是想爲中心的十字 - 這是更簡單的解決方案(沒有任何變化,除了路徑在您的按鈕):

 <Path 
      Fill="Black" 
      Stretch="Uniform" 
      Margin="7" 
      Data="M22,18L22,8 18,8 18,18 8,18 8,22 18,22 18,32 22,32 22,22 32,22 32,18z" 
      /> 

其實是Stretch屬性控制如何調整您的形狀以填充其分配的空間:MSDN