2

我有一个 WPF ListView 表,默认将列标题调整为每个标题文本的宽度。这不会均匀地填满整个表格;我在左边有一堆挤压的柱子,在右边有一个空置的空间。

我希望能够将列的大小调整为与列文本、标题或行中最长项目成比例的宽度。我该怎么做?

我目前正在做的最初是使用以下代码(其中 SearchResultsTable 是 ListView)将标题调整为与标题文本成比例的宽度:

private void Resize()
{
    var width = SearchResultsTable.ActualWidth;
    var gridView = SearchResultsTable.View as GridView;
    var columns = new GridViewColumnCollection();
    if (gridView != null)
    {
        columns = gridView.Columns;
    }
    var initialColumnWidths = columns.Sum(column => column.ActualWidth);
    var scale = width / initialColumnWidths;
    foreach (var column in columns)
    {
        column.Width = column.ActualWidth * scale;
    }
}

但是,这仅根据标题文本宽度调整大小;我不知道如何获得标题和行的最大宽度。有什么建议么?

4

1 回答 1

0

这就是我正在做的事情。希望这可以帮助。您可以在 ListView 中获取或设置 GridView 列的宽度,我使用 ListView 控件本身的宽度来增加/减少每个 ListView 列的相对宽度以按比例填充空间 - 然后扩展最后一列。

您可以做的是修改我的代码,以在我刚刚添加的条件“if(total_width < e.NewSize.Width”)中按比例分配列的总宽度和 ListView 的宽度之间的差异与最后一列宽度的差异。

LV_FileList.SizeChanged += this.onLV_FileList_SizeChanged;

...

    /// <summary>
    /// Proportionally resize listview columns when listview size changes
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void onLV_FileList_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        if ((sender is ListView) && 
            (e.PreviousSize.Width > 0))
        {
            double total_width = 0;
            GridViewColumnCollection gvcc = ((GridView)(sender as ListView).View).Columns;
            foreach (GridViewColumn gvc in gvcc)
            {
                gvc.Width = (gvc.Width / e.PreviousSize.Width) * e.NewSize.Width;
                total_width += gvc.Width;
            }

            //Increase width of last column to fit width of listview if integer division made the total width to small
            if (total_width < e.NewSize.Width)
            {
                gvcc[gvcc.Count - 1].Width += (e.NewSize.Width - total_width);
            }

            //Render changes to ListView before checking for horizontal scrollbar
            this.AllowUIToUpdate();

            //Decrease width of last column to eliminate scrollbar if it is displayed now
            ScrollViewer svFileList = this.FindVisualChild<ScrollViewer>(LV_FileList);
            while ((svFileList.ComputedHorizontalScrollBarVisibility != Visibility.Collapsed) &&  (gvcc[gvcc.Count - 1].Width > 1))
            {
                gvcc[gvcc.Count - 1].Width--;
                this.AllowUIToUpdate();
            }
        }
    }


    /// <summary>
    /// Threaded invocation to handle updating UI in resize loop
    /// </summary>
    private void AllowUIToUpdate()
    {
        DispatcherFrame dFrame = new DispatcherFrame();

        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new DispatcherOperationCallback(delegate(object parameter)
        {
            dFrame.Continue = false;
            return null;

        }), null);

        Dispatcher.PushFrame(dFrame);
    }
于 2016-09-12T15:09:28.650 回答