我为Button创建了自己的ControlTemplate,如下所示:
<Style x:Key="LightButtonStyle" TargetType="{x:Type ButtonBase}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border
x:Name="WrappingBorder"
Margin="{TemplateBinding Margin}"
...
>
<ContentPresenter
Content="{TemplateBinding Content}"
...
>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
现在,我注意到当我为我的按钮设置页边距时,就像:
<Button
Style="{StaticResource LightButtonStyle}"
Margin="20"
>
Hello world!
</Button>
这个按钮实际上有双倍的保证金- 40。实际上,我假设控件应该永远不使用phase,而during属性仅由按钮的祖先在排列阶段读取。然后,我研究了WPF的默认样式,发现没有人使用保证金。
这是正确的结论吗(只有容器才能正确地安排保证金)?换句话说,每次在我的风格中使用{TemplateBinding边距}时,我都会得到双边距吗?,是否有一些我的控件不应该使用的类似属性列表(因为它们仅用于“周围世界”)?
你能给我指一下解释这一点的MSDN页面吗?谢谢!
编辑:
我想我应该在http://msdn.microsoft.com/en-us/library/ms745058.aspx和http://msdn.microsoft.com/en-us/library/ms751709.aspx中找到答案,但我想他们并没有明确地提到是从不使用控件,而是始终是祖先或wpf系统对其进行估值并使用它来影响布局……
发布于 2010-01-05 15:37:18
您的结论是正确的,如果您查看提供的默认模板的框架,您将看到模板中的Margin
绑定到控件的Padding
属性。
发布于 2010-01-05 17:21:14
边距由布局系统自动应用,在控件模板中应该使用填充。
<Border x:Name="WrappingBorder" Margin="{TemplateBinding Padding}" ... />
https://stackoverflow.com/questions/2006872
复制相似问题