Search code examples
c#wpfxamldatagridwpfdatagrid

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. Currently the DataGrid will draw a horizontal scroll bar and make me scroll horizontally to see all the content.

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.


Solution

  • 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); I can adjust based on individual cell contents later.

    Thank you, @chancea.