Skip to main content
 首页 » 编程设计

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

2024年02月24日21zdz8207

我有一个 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.

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