gpt4 book ai didi

WPF:为什么我不应该在 ControlTemplate 中使用 {TemplateBinding Margin} - Margin 是否仅适用于元素的容器?

转载 作者:行者123 更新时间:2023-12-04 03:47:02 25 4
gpt4 key购买 nike

我为 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>

该按钮实际上具有两倍的 Margin - 40。我假设控件实际上应该从不使用 Margin,并且 Margin 属性在排列阶段仅由按钮的祖先读取。然后我查看了 WPF 默认样式,发现没有一个使用 Margin。

这是正确的结论(Margin 只能由容器正确排列)吗?换句话说,每次我在我的风格中使用 {TemplateBinding Margin} 时,我会得到双倍边距吗? 是否有一些我的控件不应该使用的类似属性列表(因为它们仅适用于“周围世界”)?

你能把我指向解释这一点的 MSDN 页面吗?谢谢!

编辑:

我想我应该在 http://msdn.microsoft.com/en-us/library/ms745058.aspx 中找到答案和 http://msdn.microsoft.com/en-us/library/ms751709.aspx ,但我认为他们没有明确提到它是 从不使用 Margin 属性的控件,即 总是 评估它并使用它来影响布局的祖先或 wpf 系统......

最佳答案

您的结论是正确的,如果您查看框架提供的默认模板,您会发现 Margin模板里面绑定(bind)了Padding控件的属性。

关于WPF:为什么我不应该在 ControlTemplate 中使用 {TemplateBinding Margin} - Margin 是否仅适用于元素的容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2006872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com