WPF:将一个组件的高度绑定到另一个组件的高度
我在一个窗口中有一个 Grid
,其中包含一个 RadioButton
、一个 TextBox
和一个 Button
,每个都位于分别为第 0、1、2 列。它们的高度都设置为自动。
然后,在窗口的另一部分,我有另一个带有 Label
、TextBox
和 Button
的 Grid
,在第 0、1 和 2 列中。高度也设置为自动。
我遇到的问题是第一个网格的高度小于第二个网格的高度。我猜这是因为 Label 强迫第二个更高。我怎样才能使第一个网格与第二个网格一样高?我尝试这样做:
将第二个网格中的文本框命名为 SomeName。
在第一个网格的
中,我将高度从“auto”更改为“{Binding ElementName=SomeName, Path=Height}”。
但这并没有达到我想要的效果。大小是一样的。我猜绑定基本上是“自动”并将其扔在那里,最终是同一件事。
另外,我正在寻找一种不涉及将高度设置为固定值的解决方案。
I have, in a window, a Grid
that contains a RadioButton
, a TextBox
and a Button
, each in column 0, 1, 2, respectively. They all have their heights set to auto.
Then, in another part of the window, I have another Grid
with a Label
, a TextBox
and a Button
, in columns 0, 1, and 2. Heights are also set to auto.
The problem I have is that the first grid's height is smaller than the second's. I guess it's because Label is forcing the second one to be taller. How can I make it so that the first grid is as tall as the second? I tried doing this:
Name the textbox in the second grid SomeName.
In the <Grid.ColumnDeclarations>
of the first Grid, I changed Height from "auto" to "{Binding ElementName=SomeName, Path=Height}".
But that didn't do what I wanted. The size was the same. I guess the Binding is basically getting "auto" and throwing it there, which ends up being the same thing.
Also, I'm looking for a solution that doesn't involve setting the heights to a fixed value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绑定到
ActualHeight
而不是Height
属性:Bind to the
ActualHeight
rather than theHeight
property:将两个网格放入共享大小范围,并使用
SharedSizeGroup
将行高锁定在一起:另请参阅 如何:在 MSDN 中的网格之间共享大小调整属性。
Put the two grids in a shared size scope, and use
SharedSizeGroup
to lock the row heights together:See also How to: Share sizing properties between grids in MSDN.