WPF Datagrid columns width as percentage of Datagrid ActualWidth
Asked Answered
R

1

7

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.

Rimose answered 26/7, 2013 at 1:39 Comment(2)
are you columns static or dynamic.Cadre
OK...some more information (now that it's later and I've had some time to look at it without my head exploding). The problem seems to be with the DataGrid inside of a listbox item template. Once in there, it's as if it doesn't get the resize event. The control itself is sized properly, but the columns simply don't update. Hopefully will be able to do a more thorough search now.Rimose
L
7

ColumnWidth property is your friend.

<DataGrid ColumnWidth="*"/>
Lowpressure answered 26/7, 2013 at 4:58 Comment(1)
I'm marking this as the answer (because it will work), and it turns out my question was badly asked. I will ask another one which will hopefully help. =)Rimose

© 2022 - 2024 — McMap. All rights reserved.