c# - 为什么这个差距会出现在我的 ListView 中?

标签 c# winforms user-interface listview

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

我遇到问题的地方是 ListView 的 GUI 端。第一次 LoadFileAttachments() 运行并填充 ListView 时,ListView 的左侧和附件之间有一个间隙。在后续调用中,间隙消失。

正如您在下面看到的,列的宽度没有变化,只是似乎有一个间隙。我 try catch 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);

最佳答案

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

The StateImageList property allows you to specify an ImageList that contains images to use to represent an application-specific state of an item in a ListView control. State images are displayed to the left of an icon for the item. You can use state images, such as checked and unchecked check boxes, to indicate application-defined item states. State images are visible in all views of the ListView control.

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

关于c# - 为什么这个差距会出现在我的 ListView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3284116/

相关文章:

c# - AngularJs 脚本在部署后不起作用,它给出错误 [$injector :unpr]

c# - 如何使用 nhibernate/linq 进行不区分大小写和串联字段搜索?

javascript - 更改 asp :TextBox css class depending on value using javascript

.net - 如何覆盖 WinForms 中数据绑定(bind)控件上的错误消息?

Java图形界面,如何轻松制作?

c# - 不能链接两个表;数据库; Entity Framework C#

winforms - 是否可以在不使用 ImageList 的情况下将图像添加到 TreeView 节点?

c# - 如何从 DataGridView 接收 "scroll box"类型的滚动事件?

android - 通过 inflate 从合并布局到 RelativeLayout 的 XML 属性

JavaFx - 在警告对话框中包装内容文本