Force resize of GridView columns inside ListView

后端 未结 5 1762
Happy的楠姐
Happy的楠姐 2020-12-10 02:07

I have a ListView WPF control with a GridView. I\'d like to resize the GridView columns when the content of the columns changes.

5条回答
  •  孤街浪徒
    2020-12-10 02:45

    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;
            }
        }
    }
    

提交回复
热议问题