[英]Implementing style for Button in WPF
我有一個快速的問題。 我已經實現了按鈕樣式。 它基本上可以工作。 這是代碼(完整示例:您可以復制和粘貼,它將起作用):
<Window x:Class="TestWPFApplication.Window5"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestWPFApplication"
mc:Ignorable="d"
Title="Window5" Height="300" Width="300">
<Window.Resources>
<Style TargetType="{x:Type Button}" x:Key="AButton">
<Setter Property="Width" Value="120"></Setter>
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Background" Value="DarkGreen" />
<Setter Property="Foreground" Value="LightGreen" />
<Setter Property="FontSize" Value="22" />
<Setter Property="FontWeight" Value="Light" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="15" Background="{TemplateBinding Background}">
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}"
HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button x:Name="QuitButton" Style="{StaticResource AButton}" Content="Quit" />
</Grid>
</Window>
沒事的 按鈕看起來像這樣:
但是,如果我稍微移動按鈕,則Margin屬性將自動生成到此...
<Button x:Name="QuitButton" Style="{StaticResource AButton}" Content="Quit" Margin="88,114,87,108.5" />
..按鈕將如下所示:
按鈕的右側和底部已被切除。 不知道為什么:/問題是:有人可以向我解釋一下嗎? 提前致謝!
您已將頂部頁邊距設置為114,底部頁邊距設置為108.5,總共為225.5,但是您還將窗口總高度設置為300。這僅為標題欄,頂部和底部窗口邊框留了77.5像素和按鈕(已設置為120像素高)。 WPF使一切變得合適的唯一方法是裁剪按鈕。 (在X軸上發生了相同的事情)。
在主窗口上設置WindowStyle="None"
和ResizeMode="NoResize"
,您會看到該按鈕現在有足夠的空間進行精細繪制。 更好的是,將右邊距和底部邊距值設置為0,現在可以將left和top設置為所需的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.