Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datagrid columns width as percentage of Datagrid ActualWidth

I've spent several hours looking at this with no results.

I simply want to have a DataGrid with X number of columns that maintains its relative width to the Grid itself.

So, for example:

  • Col 1: 10%
  • Col 2: 10%
  • Col 3: 10%

I have set up a style that is attached to a DataGrid item template. I have attempted binding the width to the DataGrid itself (often a cyclical reference), to another object (the value comes in, but the DataGridColumn doesn't update), with a Converter (the converter gets the correct value, then sets the value of the column, but despite different numbers coming back, the width of the column never changes).

I've set up a SizeChanged event on the DataGrid itself and set the width of the columns specifically, which DID change the values of the columns, but each size event caused another size event, etc... etc...

I'm obviously too stupid to figure this out myself, so if anyone could provide me a simple example of what I imagine should be a simple thing to do, I'd be ever so grateful.

So, just to be clear, ALL I'm looking for is a DataGrid with X number of columns that maintain a fixed % of the DataGrid width itself - pretty much like any table in HTML would do natively.

Thanks in advance.

like image 347
Aibynn Avatar asked Jul 26 '13 01:07

Aibynn


1 Answers

ColumnWidth property is your friend.

<DataGrid ColumnWidth="*"/>
like image 98
Nitesh Avatar answered Nov 19 '22 04:11

Nitesh