如何TemplateBind到BorderThickness.Top(或Bottom或Left或Right)?
我想知道是否可以将像 BorderThickness.Top 这样的结构元素绑定到 TemplatedParent 的相应属性。我已经尝试过,
<Border Margin="0" Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}">
<Border.BorderThickness>
<Thickness Left="0" Right="0" Top="{TemplateBinding BorderThickness.Top}" Bottom="{TemplateBinding BorderThickness.Bottom}"/>
</Border.BorderThickness>
</Border>
我想这样做的原因是我希望 Left 和 Right 为 0,并且只绑定 Top 和 Bottom。
提前致谢。
I wonder if it is possible to bind a structure element like BorderThickness.Top to TemplatedParent's corresponding property. I have tried
<Border Margin="0" Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}">
<Border.BorderThickness>
<Thickness Left="0" Right="0" Top="{TemplateBinding BorderThickness.Top}" Bottom="{TemplateBinding BorderThickness.Bottom}"/>
</Border.BorderThickness>
</Border>
The reason i want to do this is i want Left and Right to be 0 and only Top and Bottom be bound.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的,因为厚度是值类型 - 您只能在依赖项对象的依赖项属性上创建绑定。
您可以做的是像平常一样绑定 BorderThickness:
然后使用转换器返回适当修改的 Thickness:
您甚至可以使用 ConverterParameter 来指定要清除 Thickness 的哪些部分。
This is not possible because Thickness is a value-type - you can only create bindings on dependency properties of dependency objects.
What you could do is binding BorderThickness as normal:
then use a converter to return an appropriately modified Thickness:
You could even use ConverterParameter to specify which parts of the Thickness to clear.
使用转换器的解决方案是正确的。
如果您只对一个值感兴趣,则可以直接在 XAML 中执行此操作,无需使用转换器。
{TemplateBinding …}
只是{BindingrelativeSource={RelativeSource TemplatedParent} …}
的语法糖,功能有限。例如一些自定义边框:
The solution with a converter is the right one.
In the case where you are interested only in one value, you can do this directly in the XAML without a converter.
{TemplateBinding …}
is only a syntactic sugar for{Binding RelativeSource={RelativeSource TemplatedParent} …}
with a limited functionality.For example some custom border: