这就是我正在做的事情。希望这可以帮助。您可以在 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);
}