如何绑定到 RowDefinition 的高度? [英] How do I bind to a RowDefinition's height?

查看:29
本文介绍了如何绑定到 RowDefinition 的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例代码中,我试图将 GridCanvas 位置偏移其行之一的高度.有谁看到我可能做错了什么?如您所见,我尝试在 xaml 文件中将绑定移到较低位置,以防需要先定义 RowDefinitions.无论哪种方式,这似乎都无关紧要,因为 Canvas.Top 始终为 0.

In this example code, I'm trying to offset the Grid's Canvas position by the height of one of its rows. Does anyone see what I might be doing wrong? As you can see, I tried moving the binding lower in the xaml file, just in case the RowDefinitions needed to be defined first. Either way, it doesn't seem to matter because Canvas.Top is always 0.

<Canvas>
   <Grid Canvas.Top="{Binding ElementName=DetailsRow, Path=ActualHeight}">
      <Grid.RowDefinitions>
         <RowDefinition x:Name="NameRow" />
         <RowDefinition x:Name="DetailsRow" />
      </Grid.RowDefinitions>
      <Button Grid.Row="0">Button</Button>
      <Button Grid.Row="1">Button</Button>

      <!-- I expected this to maybe work, but no dice
      <Canvas.Top>
         <Binding ElementName="DetailsRow" Path="ActualHeight" />
      </Canvas.Top>
      -->

   </Grid>
</Canvas>

推荐答案

ActualHeight 不是依赖属性,因此它可能不会触发任何类型的更改通知.ActualHeight 实际上从 0 开始,直到测量网格,这可能是一种解释.与将 ActualHeight 定义为依赖属性的 FrameworkElement 不同,RowDefinition 不是从 FrameworkElement 派生的,而只是定义ActualHeight 作为没有更改事件的普通属性.

ActualHeight is not a dependency property so it's probably not triggering any kind of change notification. ActualHeight actually starts at 0 until the grid is measured so that could be one explanation. Unlike FrameworkElement, which does define ActualHeight as a dependency property, RowDefinition doesn't derive from FrameworkElement and just defines ActualHeight as a normal property with no change event.

我实际上已经考虑过这样一个事实,即应该有一个 BindingMode.Polling 选项,在该选项中绑定系统会以特定间隔轮询源属性.但不幸的是,您可能只是被困在代码中.

I've actually thought about the fact that there should be a BindingMode.Polling option where the binding system would poll the source property at certain intervals. But unfortunately you may just be stuck doing it in code.

这篇关于如何绑定到 RowDefinition 的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆