更新时间:2022-10-21 11:24:50
我的猜测是,您 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.