【发布时间】:2011-01-14 13:31:53
【问题描述】:
我在表单中遇到问题,我已按以下顺序将列添加到 .NET ListView 控件:
A | B | C | D
A-D 列的显示索引为 0-3,按该顺序显示,但显示顺序错误:
A | B | D | C
^-----^ these are switched at runtime
注意:在设计时,一切看起来都是我想要的。
我猜,但我不知道为什么,这是因为我在添加 D 列之后将 C 列添加到 ListView。我在列编辑器对话框中将其上移了一个档次,调整了显示索引,然后检查.Designer.cs文件中的创建顺序,一切都按AD顺序,按那个顺序。
但问题仍然存在。
另请注意:这不仅仅是标题标签问题,列会被交换,包括它们的数据。数据按我希望它显示的顺序添加,但最后两列被交换了。
我还需要检查什么才能弄清楚为什么我的一个列在错误的位置?
我发现了问题所在。出于某种原因,即使我在对话框中设置了 DisplayIndex 属性,也不会保留它。
如果我完全关闭了表单,然后在 Visual Studio 中重新打开它,它就会移动。显然,对话框编辑器没有将这些属性检测为“已更改”,因此保存机制也不关心为我保存它。
添加列的代码是这样的:
this.lvResult = new System.Windows.Forms.ListView();
this.colResultId = new System.Windows.Forms.ColumnHeader();
this.colResultTitle = new System.Windows.Forms.ColumnHeader();
this.colResultLanguage = new System.Windows.Forms.ColumnHeader();
this.colResultTags = new System.Windows.Forms.ColumnHeader();
//
// lvResult
//
this.lvResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lvResult.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.colResultId,
this.colResultTitle,
this.colResultLanguage,
this.colResultTags});
this.lvResult.FullRowSelect = true;
this.lvResult.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.lvResult.HideSelection = false;
this.lvResult.Location = new System.Drawing.Point(12, 6);
this.lvResult.Name = "lvResult";
this.lvResult.Size = new System.Drawing.Size(466, 117);
this.lvResult.TabIndex = 0;
this.lvResult.UseCompatibleStateImageBehavior = false;
this.lvResult.View = System.Windows.Forms.View.Details;
this.lvResult.SelectedIndexChanged += new System.EventHandler(this.lvResult_SelectedIndexChanged);
//
// colResultId
//
this.colResultId.Text = "#";
this.colResultId.Width = 35;
//
// colResultTitle
//
this.colResultTitle.Text = "Title";
this.colResultTitle.Width = 220;
//
// colResultTags
//
this.colResultTags.DisplayIndex = 2;
this.colResultTags.Text = "Tags";
this.colResultTags.Width = 100;
//
// colResultLanguage
//
this.colResultLanguage.Text = "Language";
当我直接在文件中添加缺少的属性时,它起作用了。
【问题讨论】: