DataGridTemplateColumn sizing
I have a WPF app with a DataGrid containing 10 DataGridTemplateColumns. When I bind a collection of objects to the data grid they all display fine in the grid. The only problem is that if I have I more rows than will fit on the screen without scroll bars, the columns only autosize to fit the values that are on the screen. If, for example, I scroll down and come to a row where the value of column 1 doesn't fit then column 1 will autosize to fit it. This is really annoying. Is there no way of making it autosize to fit all items in the 开发者_如何学编程collection, regardless of whether or not they are initially on screen? I've tried setting the width property of each column to Auto but that didn't do anything. Thanks in advance for any help
The issue is being caused because the rows in the datagrid are being virtualized. So items not shown on the screen do not need to be drawn (or have their sizes calculated) until they need to be displayed.
You could turn virtualization off like so:
<DataGrid VirtualizingStackPanel.IsVirtualizing="False" HorizontalScrollBarVisibility="Hidden">
Note, however, that doing so can cause your datagrid to load slower if you have a lot of items due to the fact that all items will be 'drawn' even when not shown on the screen.
精彩评论