代码之家  ›  专栏  ›  技术社区  ›  Jared Harley

为什么在我的ListView中会出现这种差距?

  •  1
  • Jared Harley  · 技术社区  · 14 年前

    我有一个C#WinForm应用程序,我在其中使用ListView显示哪些文件已上载到我的数据库。我每次都用相同的代码 LoadFileAttachments()

    加载文件附件() 运行并填充ListView时,ListView左侧和附件之间存在间隙。在随后的通话中,差距消失了。

    正如您在下面看到的,列的宽度没有改变,只是看起来有一个间隙。我尝试捕获ListView的MouseClick事件,并使用ListViewHittesInfo查看其中的内容,它显示了我单击旁边的项目,属性为“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);
    
    3 回复  |  直到 14 年前
        1
  •  3
  •   Joshua    14 年前

    我相信这个问题是由你的设置引起的 StateImageList ListView.StateImageList 文档 状态图像列表 是显示在旁边的附加图像列表 SmallImageList .

    状态图像列表

    试着评论一下,看看这是否能解决你的问题。

        2
  •  1
  •   Jacob G    14 年前

    如果显式设置 IndentCount ListViewItem 在创建它们时将其设置为0?

    我想这和 ImageList . 在设计器中,我可以通过添加和删除图像列表来获得类似的行为。直到最后一刻才解决 ListView

    我要加一个 列表视图 反复。

        3
  •  0
  •   DOK    14 年前

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

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