WPF:设置控件高度以填充网格行高

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14938074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:35:55  来源:igfitidea点击:

WPF: Set Control Height to Fill Grid Row Height

wpfgridheight

提问by dotNET

A WPF Grid control with 3 row and 3 columns. The Height of the Row in question is set to Auto. In the first two cells I have two controls with dynamic heights. In the third cell, I have another control I want to be automatically set to stretch within the Grid cell. I have tried VerticalAlignment="Stretch", but that simply sets Grid Row Height equal to control's height. What are my options here?

具有 3 行 3 列的 WPF 网格控件。相关行的高度设置为自动。在前两个单元格中,我有两个具有动态高度的控件。在第三个单元格中,我有另一个控件要自动设置为在网格单元格内拉伸。我试过VerticalAlignment="Stretch",但这只是将 Grid Row Height 设置为等于控件的高度。我在这里有哪些选择?

采纳答案by Stewbob

Not sure what kind of control you are using in Cell 3, but most of the WPF controls will automatically stretch to fit inside the Grid cell. The row height of your grid will be set by the height of the controls in Cells 1 and 2.

不确定您在 Cell 3 中使用的是哪种控件,但大多数 WPF 控件会自动拉伸以适应 Grid 单元格。网格的行高将由单元格 1 和 2 中控件的高度设置。

If you are using some kind of custom control where the default behavior for height is different, you can set Height="Auto".

如果您使用的是高度默认行为不同的某种自定义控件,则可以设置Height="Auto".

If that doesn't work either, you can do a data binding to get the actual height of the control in either cell 1 or 2. Set the Heightproperty of your control in Cell 3 to the following:

如果这也不起作用,您可以进行数据绑定以获取单元格 1 或 2 中控件的实际高度。将Height单元格 3 中控件的属性设置为以下内容:

Height="{Binding ActualHeight, ElementName=MyControlNameFromCell1, Mode=OneWay}"

EDIT

编辑

Another way that might be more robust is to do a data binding for the Height of the row. So instead of using "Auto"for the height of the row, use the data binding shown above.

另一种可能更健壮的方法是对行的高度进行数据绑定。因此,不要使用"Auto"行的高度,而是使用上面显示的数据绑定。