首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在ListView中强制调整GridView列的大小

在ListView中强制调整GridView列的大小
EN

Stack Overflow用户
提问于 2009-05-10 12:45:10
回答 5查看 12.1K关注 0票数 17

我有一个带有GridViewListView WPF控件。我希望在列的内容发生变化时调整GridView列的大小。

我有几个不同的数据集,但是当我从一个更改到另一个时,每个列的大小都适合以前的数据。我想要动态更新。我该怎么做呢?

EN

回答 5

Stack Overflow用户

发布于 2009-06-17 15:07:38

最后,关于这一点的一些结果。我已经找到了一种自动调整大小的方法,就像最初在双击列标题上的抓取器时所做的那样。

代码语言:javascript
复制
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;
        }
    }
}
票数 29
EN

Stack Overflow用户

发布于 2012-11-12 18:25:33

构建在Oskars answer之上,这是一个混合行为,可以在内容更改时自动调整列的大小。

代码语言:javascript
复制
 /// <summary>
 /// A <see cref="Behavior{T}"/> implementation which will automatically resize the automatic columns of a <see cref="ListView">ListViews</see> <see cref="GridView"/> to the new content.
 /// </summary>
 public class GridViewColumnResizeBehaviour : Behavior<ListView>
 {
      /// <summary>
      /// Listens for when the <see cref="ItemsControl.Items"/> collection changes.
      /// </summary>
      protected override void OnAttached()
      {
          base.OnAttached();

          var listView = AssociatedObject;
          if (listView == null)
              return;

          AddHandler(listView.Items);
      }

      private void AddHandler(INotifyCollectionChanged sourceCollection)
      {
          Contract.Requires(sourceCollection != null);

          sourceCollection.CollectionChanged += OnListViewItemsCollectionChanged;
      }

      private void RemoveHandler(INotifyCollectionChanged sourceCollection)
      {
          Contract.Requires(sourceCollection != null);

          sourceCollection.CollectionChanged -= OnListViewItemsCollectionChanged;
      }

      private void OnListViewItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
      {
          var listView = AssociatedObject;
          if (listView == null)
              return;

          var gridView = listView.View as GridView;
          if (gridView == null)
              return;

          // If the column is automatically sized, change the column width to re-apply automatic width
          foreach (var column in gridView.Columns.Where(column => Double.IsNaN(column.Width)))
          {
               Contract.Assume(column != null);
               column.Width = column.ActualWidth;
               column.Width = Double.NaN;
          }
      }

      /// <summary>
      /// Stops listening for when the <see cref="ItemsControl.Items"/> collection changes.
      /// </summary>
      protected override void OnDetaching()
      {
          var listView = AssociatedObject;
          if (listView != null)
              RemoveHandler(listView.Items);

          base.OnDetaching();
      }
 }
票数 5
EN

Stack Overflow用户

发布于 2009-07-29 14:32:44

难道没有一种方法可以绑定到列的ActualWidth吗?类似于:

代码语言:javascript
复制
<GridViewColumn x:Name="column1" Width="{Binding ElementName=column1, Path=ActualWidth}" />

我已经尝试过了,它似乎只在第一次有效。无绑定错误。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/845269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档