我有一个简单的问题。我已经实现了按钮样式。它基本上可以工作。这里是代码(完整的例子:你可以复制和粘贴,它会工作):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" />
..按钮看起来就像这样:
按钮的右侧和底侧已被切断。不知道为什么:/ 问题是:任何人都可以解释我这个吗?提前致谢!
尝试从ContentPresenter中删除'Margin =“0,0,0,0”'并查看是否可以帮助您 –
@MohitShrivastava感谢您的关注,但它没有帮助:/ – tobiasznowak
永远不要浪费你的时间使用表单设计器来定位事物。它总是把它拧紧。我认为问题在于,包含该按钮的网格没有被放大以在该新位置为该按钮腾出空间。删除它添加到按钮上的损坏的保证金值,并通过其他方式进行定位。网格行/列,StackPanel中,WrapPanel,的HorizontalAlignment等 –