WPF:将组件的高度绑定到另一个组件的高度 [英] WPF: Binding the height of a component to another's
问题描述
我在窗口中有一个 Grid
,其中包含一个 RadioButton
和一个 TextBox
和 Button
,分别位于第0、1、2列中。
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.
然后,在窗口的另一部分,我还有另一个 Grid
带有 Label
, TextBox
和 Button
列0、1和2。高度也设置为自动。
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.
我遇到的问题是第一个网格的高度小于第二个网格的高度。我猜是因为Label强迫第二个更高。如何使第一个网格与第二个网格一样高?我尝试这样做:
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:
在第二个网格中为文本框命名SomeName。
在< Grid.ColumnDeclarations>中。
在第一个网格中,我将高度从自动更改为 {Binding ElementName = SomeName,Path = Height}。
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}".
但这并没有满足我的要求。大小是一样的。我猜Binding基本上是自动的,然后扔到那里,结果还是一样。
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.
推荐答案
将两个网格放在共享大小范围,并使用 SharedSizeGroup
可以将行高锁定在一起:
Put the two grids in a shared size scope, and use SharedSizeGroup
to lock the row heights together:
<SomeContainer Grid.IsSharedSizeScope="True"> <!-- Could be the Window or some more nearby Panel -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="LabelAndRadioButtonGroup" />
</Grid.RowDefinitions>
<Label Grid.Row="0" />
</Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="LabelAndRadioButtonGroup" />
</Grid.RowDefinitions>
<RadioButton Grid.Row="0" />
</Grid>
</SomeContainer>
这篇关于WPF:将组件的高度绑定到另一个组件的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!