除了使用转换器之外,还有其他方法可以在 xaml 元素绑定语句中进行简单计算吗?
在 XAML 中,我想将一个元素的高度绑定为另一个元素高度的一半。 有没有一种方法可以做到这一点,而不涉及在代码隐藏中编写转换器?
例子:- 我有什么……
<Button Name="RemoveButton" Content="Remove Stage" Width="100" Height="{Binding ElementName=AddButton, Path=Height, Converter={StaticResource MyHalfHeightConverter}}"/>
我想要什么……
<Button Name="RemoveButton" Content="Remove Stage" Width="100" Height="{Binding ElementName=AddButton, Path=(Height / 2.0)}"/>
In XAML I want to bind the height of one element to be half the height of another element.
Is there a way to do this that doesn't involve writing a converter in the code-behind?
Example:-
What I've got...
<Button Name="RemoveButton" Content="Remove Stage" Width="100" Height="{Binding ElementName=AddButton, Path=Height, Converter={StaticResource MyHalfHeightConverter}}"/>
What I'd like...
<Button Name="RemoveButton" Content="Remove Stage" Width="100" Height="{Binding ElementName=AddButton, Path=(Height / 2.0)}"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有转换器就没有绑定解决方案。但为什么不使用一个呢?您经常会遇到这样的需求,因此创建某种带有某些属性或参数的 MathConverter 是有意义的。这样您就不需要为每个需求创建单独的转换器。
但是,如果您确实不想使用转换器,则根据您的布局,您还可以使用星形网格,其中 AddButton 分布在两行中,而 RemoveButton 仅占据一行:
如果您希望 RemoveButton 居中垂直方向,使用这个:
这样,AddButton 占据三行(总共 4*),而 RemoveButton 位于中间行(2*)。
如果无法将它们添加到一个共享网格,您可以使用 Grid.IsSharedSizeScope 附加属性。详细信息可以在此处找到。
I do not think there is a Binding solution without a converter. But why not use one? You will often come across a requirement like this, so it makes sense to create some sort of MathConverter which takes some properties or parameters. Then you do not need to create a separate converter for each single requirement.
However, if you really do not want to use a converter, depending on your layout you might also use a star-sized grid where the AddButton is spread across two rows while the RemoveButton only occupies one row:
If you want the RemoveButton to be centered vertically, use this:
This way, the AddButton occupies three rows (4* in total) while the RemoveButton is in the center row (2*).
If it is not possible to add them to one shared Grid, you might make use of the
Grid.IsSharedSizeScope
attached property. Details can be found here.