为什么我的ListView不能正确显示Details视图?
所以,我有一个列表视图,如问题标题所示。我有两列设置:姓名和修改日期。这些都是在设计师加入,这里是由设计师参考发出的代码:为什么我的ListView不能正确显示Details视图?
// lstFiles
this.lstFiles.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.clmName,
this.clmDate});
// ...
// clmName
this.clmName.Text = "Name";
this.clmName.Width = 105;
// clmDate
this.clmDate.Text = "Modified";
this.clmDate.Width = 128;
在设计,这个看上去很美。
列表项本身是一个ListViewItem的一个微小的子类,简单地提取一些元数据从一个文件(在这种情况下,修改日期),并增加了一个分项目本身:
class GalleryItem : ListViewItem {
public string File;
public DateTime DateModified;
public GalleryItem(string file) : base(Path.GetFileNameWithoutExtension(file)) {
this.ImageKey = Path.GetExtension(file);
File = file;
DateModified = System.IO.File.GetLastWriteTime(file);
this.SubItems.Add(DateModified.ToString());
}
}
要添加项目清单,我只是这样做:
lstFiles.BeginUpdate();
lstFiles.Clear();
foreach (String f in files) {
ListViewItem lvi = new GalleryItem(f);
lvi.Group = lstFiles.Groups["grpFiles"]; //this varries
//omitted: check/add icon to list
lstFiles.Items.Add(lvi);
}
lstFiles.EndUpdate();
所以,这一切都为大图标视图的伟大工程,等:
然而,它打破了上详细查看:
有在列表项目(有一个滚动条)。如果您大致点击红色箭头下的列(添加在绘画中),则会选择一个项目(右上方区域为图像预览),但不会看到任何选定内容。
总之,我做错了什么?
我刚刚刮起了样品测试:
using System;
using System.Windows.Forms;
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var G1 = new ListViewGroup("Group 1");
var G2 = new ListViewGroup("Group 2");
Application.Run(new Form {
Controls = {
new ListView {
Dock = DockStyle.Fill,
Groups = { G1, G2 },
View = View.Details,
//Columns = { "First", "Second" },
Items = {
new ListViewItem { Text = "One", Group = G1, SubItems = { "1" } },
new ListViewItem { Text = "Two", Group = G2, SubItems = { "2" } },
new ListViewItem { Text = "Three", Group = G2, SubItems = { "3" } },
},
},
},
});
}
}
你会发现,它复制了问题。如果您取消注释创建其工作的列的行。这表明你的专栏不存在。
虽然打字答案突然出现在我的脑海:
要调用ListView.Clear
代替ListView.Items.Clear
所以你在代码中移除列。
这是为什么适当的睡眠对于高质量代码来说至关重要,非常感谢! – 2011-02-26 22:08:59
很好的结果!错过了。 – XIVSolutions 2011-02-27 20:58:39
我的理解是,您无法在详细信息视图中使用组。我现在不能测试这个,所以我会直接回忆。但是,尝试在没有组的情况下填充列表并查看会发生什么。我强烈怀疑造成问题的是集团的一部分。
我试图根据您的建议删除与组相关的代码,但我仍然得到一样的问题。 :( – 2011-02-26 16:40:17
@SIVSolutions:可以在Details View模式下使用Groups – CharithJ 2011-05-22 23:49:03
我有Tergiver发现的完全相同的问题,但还有一个问题 - 当您添加列时,ListView必须处于View.Details模式。
这个红色箭头从哪里来?你不能只是重写OnPaint和/或与UserPaint风格混淆。 – 2011-02-26 17:01:02
请确保您阅读了整个问题:“如果您大致点击**红色箭头(添加在绘图中)的列**” – 2011-02-26 17:03:30