【发布时间】:2015-04-02 10:00:44
【问题描述】:
我正在尝试学习自定义 WPF Buttons。
之前我将图像用作Button 的Content,但我想将其更改为基于XAML 的对象。
我正在尝试将Path 放入Button 的Content,但无法正确设置。
我的版本在下面
<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 正常工作:
我正在尝试得到这样的结果:
【问题讨论】: