如何按比例调整WPF Listview的大小?

调整Listview控件的大小有点问题。 我打算根据窗口的实际大小按比例resize。

对于Listview的初始大小,它是300 X 600(宽X高),以像素为单位。 我还将其maxHeight设置为750,但其宽度保持不变,即300。

此外,在Wondow的属性中,我已将SizeToContent属性更改为WidthAndHeight ,因为有些线程建议您在调整窗口大小后让系统决定控件的正确大小。

但是,它尚未奏效。 所以我来这里寻求帮助。 谢谢。

无论如何我们可以在WPF中设置宽度和高度的百分比值吗? 如果我被允许使用百分比,例如身高= 80%,那就容易多了。

编辑:

为了更清楚,这里是xaml中的通用代码结构

            

如您所见,我目前正在使用2个堆栈面板并将它们放在不同的行中。 但即使我改变了,Listview仍然无法按比例resize。

将ListView放在网格中,并使用列的“*”宽度function:

      ...  

此示例中的列0的宽度为“4 *”,列1的默认宽度为“1 *”。 这意味着它们之间的宽度为“五颗星”,第0列则为四条。 这样可以获得80%的宽度。

只是对我发现有效的东西的一个FYI

这是一个可以与ListView一起使用并允许星列大小调整的值转换器

来自文章:

 ///  /// Calculates the column width required to fill the view in a GridView /// For usage examples, see http://leghumped.com/blog/2009/03/11/wpf-gridview-column-width-calculator/ ///  public class WidthConverter : IValueConverter { ///  /// Converts the specified value. ///  /// The parent Listview. /// The type. ///  /// If no parameter is given, the remaning with will be returned. /// If the parameter is an integer acts as MinimumWidth, the remaining with will be returned only if it's greater than the parameter /// If the parameter is anything else, it's taken to be a percentage. Eg: 0.3* = 30%, 0.15* = 15% ///  /// The culture. /// The width, as calculated by the parameter given public object Convert(object value, Type type, object parameter, CultureInfo culture) { if(value == null) return null; ListView listView = value as ListView; GridView grdView = listView.View as GridView; int minWidth = 0; bool widthIsPercentage = parameter != null && !int.TryParse(parameter.ToString(), out minWidth); if(widthIsPercentage) { string widthParam = parameter.ToString(); double percentage = double.Parse(widthParam.Substring(0, widthParam.Length - 1)); return listView.ActualWidth * percentage; } else { double total = 0; for(int i = 0; i < grdView.Columns.Count - 1; i++) { total += grdView.Columns[i].ActualWidth; } double remainingWidth = listView.ActualWidth - total; if(remainingWidth > minWidth) { // fill the remaining width in the ListView return remainingWidth; } else { // fill remaining space with MinWidth return minWidth; } } } public object ConvertBack(object o, Type type, object parameter, CultureInfo culture) { throw new NotSupportedException(); } } 

如果你没有参数调用它,它将占用ListView中的剩余宽度:

 // fills remaining width in the ListView  

如果使用整数作为参数,则该值将作为最小宽度

 // fills remaining width in the ListView, unless the remaining width is less than the parameter  

或者,您可以使用星号指定GridView类型宽度,并返回ListView的百分比宽度

 // calculates 30% of the ListView width  

这是我用来按比例调整WPF ListView列的大小,以便在resize后不会显示水平滚动条。 这可以处理任意数量的列,以及垂直滚动条的存在。 没有使用转换器,只更改了大小的事件处理程序。 到目前为止,这一点运作良好。 唯一的缺点是当用户调整窗口大小时,水平滚动条有时会​​闪烁。

 LV_FileList.SizeChanged += this.onLV_FileList_SizeChanged; 

  ///  /// Proportionally resize listview columns when listview size changes ///  ///  ///  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(LV_FileList); while ((svFileList.ComputedHorizontalScrollBarVisibility != Visibility.Collapsed) && (gvcc[gvcc.Count - 1].Width > 1)) { gvcc[gvcc.Count - 1].Width--; this.AllowUIToUpdate(); } } } ///  /// Threaded invocation to handle updating UI in resize loop ///  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); }