I have a ListView WPF control with GridView which I'd like to resize the GridView columns, when the content of the columns changes. I have several distinct data sets but when I change from one to another, the size of each columns fits the previous data. I'd like to update dynamically. How can I do that?
A:
you could measure the longest string in pixels and then adjust the column widths accordingly:
Graphics graphics = this.CreateGraphics();
SizeF textSize = graphics.MeasureString("How long am I?", this.Font);
if you create an algorithm for sizing each column as a ratio of these lengths you should get a good result.
Richard
2009-05-10 12:57:59
Well I suppose this won't work with WPF and binding...
2009-05-10 14:36:20
+6
A:
Finally, some results on this one. I've found a way to do the same auto-sizing that is done initially and when the gripper on a column header is double clicked.
public void AutoSizeColumns()
{
GridView gv = listView1.View as GridView;
if (gv != null)
{
foreach (var c in gv.Columns)
{
// Code below was found in GridViewColumnHeader.OnGripperDoubleClicked() event handler (using Reflector)
// i.e. it is the same code that is executed when the gripper is double clicked
if (double.IsNaN(c.Width))
{
c.Width = c.ActualWidth;
}
c.Width = double.NaN;
}
}
}
Oskar
2009-06-17 15:07:38
A:
Isn't there a way to bind to the ActualWidth of the column? Something like :
<GridViewColumn x:Name="column1" Width="{Binding ElementName=column1, Path=ActualWidth}" />
I have tried this and it works only the first time, it seems. No binding error.
Thia
2009-07-29 14:32:43
I have also tried :Width="{Binding Path=ActualWidth, RelativeSource={x:Static RelativeSource.Self}}"
Thia
2009-07-29 17:01:42