Auto resize columns in WPF TreeListView
I am trying to auto resize the c开发者_StackOverflowolumns of WPF TreeListView (http://www.codeproject.com/KB/WPF/wpf_treelistview_control.aspx) using this code:
public void AutoResizeColumns()
{
GridView gv = this.View as GridView;
if (gv != null)
{
foreach (GridViewColumn gvc in gv.Columns)
{
if (double.IsNaN(gvc.Width))
gvc.Width = gvc.ActualWidth;
gvc.Width = double.NaN;
}
}
}
But when I resize it, the column width is not accounting for the margin of the row and the words are cut off by like 10px and then if I double click the column, it will resize without cutting off the words.
I have also tried this with no luck:
public void AutoResizeColumns()
{
GridView gv = this.View as GridView;
if (gv != null)
{
foreach (GridViewColumn gvc in gv.Columns)
{
gvc.Width = gvc.ActualWidth + 10;
}
}
}
Does anyone know how to fix this?
After hours of trying to figure this out, I finally got it. The column Width is being set to ActualWidth which is less then its supposed to be, so if I set the column Width to double.MaxValue then when its set to double.NaN it will resize to the "real" actual width.
Here's the code:
public void AutoResizeColumns()
{
GridView gv = this.View as GridView;
if (gv != null)
{
foreach (GridViewColumn gvc in gv.Columns)
{
// Set width to highest possible value
gvc.Width = double.MaxValue;
// Set to NaN to get the "real" actual width
gvc.Width = double.NaN;
}
}
}
精彩评论