【问题标题】:Auto resize columns in WPF TreeListView自动调整 WPF TreeListView 中的列大小
【发布时间】:2011-05-21 09:36:44
【问题描述】:

我正在尝试使用此代码自动调整 WPF TreeListView (http://www.codeproject.com/KB/WPF/wpf_treelistview_control.aspx) 的列大小:

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

但是当我调整它的大小时,列宽不考虑行的边距,并且单词被切断了 10px,然后如果我双击该列,它会调整大小而不切断单词。

我也试过这个,但没有运气:

    public void AutoResizeColumns()
    {
        GridView gv = this.View as GridView;

        if (gv != null)
        {
            foreach (GridViewColumn gvc in gv.Columns)
            {

                gvc.Width = gvc.ActualWidth + 10;
            }
        }
    }

有谁知道如何解决这个问题?

【问题讨论】:

    标签: wpf c#-4.0 resize treelistview


    【解决方案1】:

    经过几个小时的尝试,我终于明白了。列 Width 被设置为 ActualWidth 小于它应该是的,所以如果我将列 Width 设置为 double.MaxValue 那么当它设置为 double.NaN 时,它将调整为“真实”实际宽度。

    代码如下:

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

    【讨论】:

      猜你喜欢
      • 2012-07-03
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 2014-03-09
      相关资源
      最近更新 更多