2015-04-02 98 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