【发布时间】: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