简体   繁体   English

如何适合WPF DataGrid列宽以适合屏幕上的整个行?

[英]How to fit WPF DataGrid column widths to fit the entire row on screen?

I have a WPF DataGrid with many text cells per row. 我有一个WPF DataGrid,每行有很多文本单元格。 Currently the DataGrid will draw a horizontal scroll bar and make me scroll horizontally to see all the content. 当前,DataGrid将绘制一个水平滚动条,并使我水平滚动以查看所有内容。

I simply want to automatically size the columns in such a way to fit all within the current window or screen. 我只是想自动调整列的大小,使其适合当前窗口或屏幕中的所有列。 I don't care if the text wraps, that's totally fine--in fact I've already set text wrapping and it works great if I manually shrink the columns. 我不在乎文本是否自动换行,这完全没问题-实际上,我已经设置了自动换行,并且如果我手动缩小列,效果很好。 Even if I just made every column the same size, that's fine so long as they all fit on screen without the need for a horizontal scroll bar. 即使我将每一列都设置为相同的大小,也可以,只要它们都适合在屏幕上显示而无需水平滚动条即可。

I've searched for a while and seen endless ways to resize content which don't apply to what I'm trying to solve. 我搜索了一段时间,发现了无数种调整内容大小的方法,这些方法不适用于我要解决的问题。 I'm fine using code-behind if needed, by the way. 顺便说一下,如果需要的话,我可以使用代码隐藏。

The solution, since I'm required to use code-behind for my project, was to set the Width of each column to new DataGridLength(1, DataGridLengthUnitType.Star); 由于我的项目需要使用后台代码,因此解决方案是将每列的Width设置为new DataGridLength(1, DataGridLengthUnitType.Star); I can adjust based on individual cell contents later. 以后我可以根据单个单元格的内容进行调整。

Thank you, @chancea. 谢谢@chancea。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM