我有一个C#WinForm应用程序,我正在使用ListView来显示哪些文件已上传到我的数据库。我每次使用相同的代码,当表单加载时调用LoadFileAttachments()
,并且每当我刷新列表时,或者从数据库添加或删除附加附件时,都会再次调用LoadFileAttachments()
。 (这部分工作很好)为什么在我的ListView中显示出这个差距?
我遇到问题的地方是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属性是否打开过? – 2010-07-19 19:37:29
@Hans不,我从来没有用过这个ListView的CheckBox,并且那个ListViewGroup不应该在那里!我不知道为什么我把它放在那里(可能试图在某些时候做一些不同的事情),但我从来不会这样做。谢谢你的追捕 - 在这里和我的来源,同样的问题。 – 2010-07-19 19:45:33
将视图设置为LargeIcon并返回到细节将非常稳固地将它击倒在头上。 – 2010-07-19 19:47:55