我创建了一个由边框和内容展示器组成的按钮模板。样式然后缠绕在此模板上并应用于按钮。但是,使用此按钮时,它不具有水平和垂直对齐的值。在设计器中,对齐显示和按钮位于正确的位置,但是当我运行该程序时,该按钮似乎具有水平对齐=左对齐和垂直对齐=右对齐。有任何想法吗?
下面是模板的代码:WPF按钮模板对齐方式设置不正确
<ControlTemplate TargetType="Button" x:Key="DefaultButtonTemplate">
<Border CornerRadius="4"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}">
<Grid>
<ContentPresenter ContentSource="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Grid>
</Border>
</ControlTemplate>
下面是样式代码:
<Style TargetType="Button">
<Setter Property="Background" Value="{DynamicResource WindowHeaderBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource WindowBorderBrush}" />
<Setter Property="Template" Value="{DynamicResource DefaultButtonTemplate}" />
<Setter Property="BorderThickness" Value="1" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource ButtonHoverBrush}" />
<Setter Property="BitmapEffect" Value="{DynamicResource ButtonHoverGlow}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource ButtonPressedBrush}" />
</Trigger>
</Style.Triggers>
</Style>
这里是按钮的代码:
<Button Name="button1" Height="31" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,10,10">Button</Button>
有文件后面没有代码
此代码的结果是一个按钮出现在父母的左上角平靠着边缘
我认为看到代码隐藏和标记在这里会有所帮助。 – Charlie 2010-05-11 23:33:16
我会发布它,但没有代码背后 – TerrorAustralis 2010-05-11 23:35:54