Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF DataGrid with variable row heights

Tags:

wpf

datagrid

I am programmatically creating a DataGrid and need the row heights to be variable so that rows with multiple lines of text have a great height to accomodate the additional lines.

I tried setting the RowHeight property in code, but this requires a fixed value. If I leave RowHeight unset it just uses an arbitrary value that does not expand if the text doesn't fit.

I would also like to have my DataGrid contents to be scaleable, so if the DataGrid is resized the actual cell resize as well, including the font inside the cells. I could use a Viewbox for this, but I've had issues with the Viewbox as it will not grow to fit the text, it shirks/expands the text to fit in it.

Perhaps I need to walk through all of the rows and columns on a resize and set the height property manually.

like image 805
WPFNewbie Avatar asked Jul 20 '11 14:07

WPFNewbie


1 Answers

Great answer to this problem from Tom - came in very useful just now. If you want to acheive this purely in Xaml without code behind, you can add the following property in your DataGrid declaration RowHeight="{x:Static sys:Double.NaN}"

To get the 'sys' namespace recognised here, you would have to addxmlns:sys="clr-namespace:System;assembly=mscorlib" to the xmlns declarations at the top of your Xaml file.

like image 162
pandrew Avatar answered Oct 16 '22 14:10

pandrew