[英]WPF Button template
我正在嘗試學習自定義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正常工作:
我正在嘗試獲得這種結果:
不確定,如果您只需要居中十字,則需要該橫梁-這是更簡單的解決方案(除了Button中的Path之外,沒有其他任何更改):
<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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.