0
votes

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.

1
So you have tried setting each columns width to star ? (Width="*") - chancea
Or <DataGrid MaxColumnWidth="50">… or however big you want it - Jerry
@chancea I wanted to try that but I have to use code-behind for my solution, and wasn't sure how to do that in code since there isn't a clear option. There is DataGridLength.Auto, but Auto means something different from *. - vargonian
@Jerry That will work fine for fixed widths in theory but I want it to stretch to fit the screen or perhaps window. - vargonian
oh for code behind you can do Width = new DataGridLength(1, DataGridLengthUnitType.Star); or again as jerry pointed out - however wide you want each column, so you can do say Width = new DataGridLength(50, DataGridLengthUnitType.Pixel); - chancea

1 Answers

0
votes

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.