How to Resize Dynamically width of a WPF DataGrid Column?

Gustavo Gonçalves picture Gustavo Gonçalves · Dec 12, 2012 · Viewed 10.1k times · Source

I Have a DataGrid in my Window, and i need to automatically resize the column's width to fit content. But i have an obstacle:

  • If his content is smaller than his header, the size of the header should prevail;
  • His content is automatically update by a Binding, linked to a TextBox. So... when user focuses to another field, the column representing that field will update. It already happens when the newest content is bigger than the previous data.
  • I'm using MVVM, and this component its a UserControl extended of a DataGrid. I can't use some resources than we have in Code-Behind, but i could use (it's not a best way to do) MVVM Binding to resize it. I really want do make a generic solution.

I'll appreciate for any help.

Best regards,

Gustavo.

Answer