【问题标题】:Why is this gap showing up in my ListView?为什么这个差距会出现在我的 ListView 中?
【发布时间】:2011-03-18 01:56:39
【问题描述】:

我有一个 C# WinForm 应用程序,我在其中使用 ListView 来显示哪些文件已上传到我的数据库。我每次都使用相同的代码,在表单加载时调用LoadFileAttachments(),并在我刷新列表或从数据库中添加或删除其他附件时再次调用。 (这部分效果很好)

我遇到的问题是 ListView 的 GUI 端。 LoadFileAttachments()第一次运行并填满ListView,ListView左侧和附件之间有空隙。在随后的调用中,间隙消失了。

正如您在下面看到的,列的宽度没有改变,只是似乎有一个间隙。我尝试捕获 ListView 的 MouseClick 事件并使用 ListViewHitTestInfo 来查看那里有什么,它显示了我在旁边单击的项目,其属性为“Selected = false”。单击图标或文本会导致项目被选中,但不在间隙中。

造成差距的原因是什么?

截图:

Screenshot of the gap/no gap http://img831.imageshack.us/img831/4054/fileattachments.png

我每次调用的代码:

private void LoadFileAttachments()
{
    attachmentListView.Items.Clear();
    ImageList iconList = new ImageList();
    attachmentListView.LargeImageList = iconList;
    attachmentListView.SmallImageList = iconList;
    attachmentListView.StateImageList = iconList;

    FileAttachmentInfo[] fileAttach = dbAccess.RetrieveAttachedRecords(loadPNGid.Value);
    foreach (FileAttachmentInfo file in fileAttach)
    {
        ListViewItem item = new ListViewItem(file.FileName);
        item.Tag = file.RowID;
        iconList.Images.Add(file.FileExtention, ExtractIcons.GetIconImage(file.FileExtention));
        item.ImageKey = file.FileExtention;
        item.SubItems.Add(GetFileTypeDescriptors.GetFileDescriptor(file.FileExtention));
        item.SubItems.Add(Conversions.FileSizeToString(file.FileSize));
        item.SubItems.Add(file.DateAdded.ToShortDateString());
        attachmentListView.Items.Add(item);
    }

    if (attachmentListView.Columns.Count == 0)
    {
        attachmentListView.Columns.Add("Attachment", 150);
        attachmentListView.Columns.Add("File type", -2);
        attachmentListView.Columns.Add("Size", -2);
        attachmentListView.Columns.Add("Date added", -2);
    }
}

这是设计器文件中的代码:

// 
// attachmentListView
// 
this.attachmentListView.AllowColumnReorder = true;
this.attachmentListView.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.attachmentListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.attachmentListView.Location = new System.Drawing.Point(0, 0);
this.attachmentListView.MultiSelect = false;
this.attachmentListView.Name = "attachmentListView";
this.attachmentListView.Size = new System.Drawing.Size(440, 301);
this.attachmentListView.TabIndex = 0;
this.attachmentListView.TileSize = new System.Drawing.Size(188, 130);
this.attachmentListView.UseCompatibleStateImageBehavior = false;
this.attachmentListView.View = System.Windows.Forms.View.Details;
this.attachmentListView.DoubleClick += new System.EventHandler(this.attachmentListView_DoubleClick);
this.attachmentListView.MouseClick += new System.Windows.Forms.MouseEventHandler(this.attachmentListView_MouseClick);

【问题讨论】:

  • 为什么要创建ListViewGroup? LV 的 CheckBoxes 属性是否打开过?
  • @Hans 不,我从来没有在这个 ListView 中使用过 CheckBoxes,而且 ListViewGroup 不应该在那里!我不知道为什么我把它放在那里(可能试图在某个时候做一些不同的事情),但我从来没有打电话给它。感谢您发现这一点 - 在这里和从我的来源中删除,同样的问题。
  • 将视图设置为 LargeIcon 并返回到 Details 会非常可靠。

标签: c# winforms user-interface listview


【解决方案1】:

如果你指定了水平对齐方式会有什么不同吗?

attachmentListView.Columns.Add("Name", -2, HorizontalAlignment.Left);

【讨论】:

  • 刚刚试了一下,没有任何变化。不过,我在我的问题中犯了一个错误——当我点击间隙时的命中测试确实显示了我正在点击旁边的项目。现在正在编辑问题...
【解决方案2】:

如果在创建 ListViewItem 时将 IndentCount 显式设置为 0,是否会有所不同?

基于调查的新答案

我认为这与ImageList 有关。在设计器中,我可以通过添加和删除图像列表来获得类似的行为。在重构 ListView 之前,它不会得到解决。

我会在开头添加一个ImageListListView,然后简单地重复清除和滋润相同的ImageList

【讨论】:

  • 有趣的想法。当我将它显式设置为零时没有任何效果,但是当我将它设置为 1 时会有所不同。当我将每个项目上的 IndentCount 设置为 1 时,第一次加载列表时,它就是项目所在的位置,如果我将缩进设置为 2。刷新后,它会移动到 Indent=1 的位置,这就是列表第一次加载时的样子。就像列表中的项目有一个消失的缩进!
  • 可以看到ListView的初始设置吗? attachmentListView 被实例化的地方......(不认为它会有所作为,但以防万一。
  • 将其添加到我的帖子底部
【解决方案3】:

我认为问题是由您对 StateImageList 属性的设置引起的。根据ListView.StateImageList 文档,StateImageList 是与SmallImageList 一起显示的附加图像列表。

StateImageList 属性允许您指定一个 ImageList,其中包含用于表示 ListView 控件中项目的特定于应用程序状态的图像。状态图像显示在项目图标的左侧。您可以使用状态图像(例如选中和未选中复选框)来指示应用程序定义的项目状态。状态图像在 ListView 控件的所有视图中都可见。

尝试将其注释掉,看看是否能解决您的问题。

【讨论】:

  • 做到了!我不确定为什么在两次运行代码后问题就消失了(因为 StateImageList 被构建了两次),但这首先阻止了它的发生。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
相关资源
最近更新 更多