如何将控制高度数据绑定到另一个控件的高度? [英] How to databind control height to another control's height?
问题描述
如果我做了类似< Canvas Height ={Binding Height,ElementName = AnotherControl}/ >
它实际上没有做任何事情,高度变为零。 输出面板不会抱怨任何绑定错误,因此AnotherControl.Height真的存在。我尝试绑定到ActualHeight,但它也不做任何事情。
还有其他我错过的东西?
我的猜测是,您 AnotherControl
未明确给出 Height
。不幸的是,在WinRT中(与WPF不同,但与Silverlight不同), ActualWidth
和 ActualHeight
是所谓的计算属性。这意味着更改事件的属性不会在内部引起改变。因此,绑定到它们是不可靠的,正如你所注意到的,它不会奏效。
旁注:它可能会不时工作,但这纯粹是因为获取调用绑定框架的时间到 ActualHeight
。
因此,您只能使用XAML 不能进行操作。您必须在代码隐藏中处理 ActualControl.SizeChanged
事件,并将 Height
设置为 AnotherControl.ActualHeight
显式。
I'm trying to have 2 controls have the same height. Can I do it with XAML only?
If I did something like <Canvas Height="{Binding Height, ElementName=AnotherControl}" />
it doesn't actually do anything and the height goes to zero. The Output panel doesn't complain about any binding errors so AnotherControl.Height really exists. I tried binding to ActualHeight but it doesn't do anything either.
Anything else I missed?
My guess is that you AnotherControl
is not explicitly given a Height
. Unfortunately, in WinRT (unlike WPF, but the same as Silverlight), ActualWidth
and ActualHeight
are what are known as "calculated properties". This means that a property changed event doesn't internally get raised when they change. As a result, binding to them is not reliable, and as you've noticed, it wouldn't quite work.
Side note: it may work from time to time, but that is purely because of the timing of the get call the binding framework makes to ActualHeight
.
So as it stands, you cannot do it with XAML only. You have to handle the ActualControl.SizeChanged
event in code-behind, and set the Height
to AnotherControl.ActualHeight
explicitly.
这篇关于如何将控制高度数据绑定到另一个控件的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!