为什么SubItems.Clear()也删除Name属性? [英] Why does SubItems.Clear() also delete the Name attribute?
问题描述
我使用的WinForms ListView的详细模式(.NET 4.0,在Windows 7上运行),我有一个函数,需要明确的子项中的特定项目。不幸的是,当我这样做,它也清楚的名称:
I am using a WinForms ListView in details mode (.NET 4.0, running on Windows 7) and I have a function that needs to clear the subitems in a particular item. Unfortunately when I do that it also clear's the name:
item.Name = "TESTNAME";
item.SubItems.Clear();
MessageBox.Show(item.Name); //shows nothing
在调试器,我跟踪它到这一点,我已经看了<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.listviewsubitemcollection.aspx"相对=nofollow> MSDN上文件,它是无益的,
In the debugger I've tracked it down to this, and I've looked at the documentation on MSDN and it's unhelpful,
清除:删除所有子项,并从集合父的ListViewItem
Clear: Removes all subitems and the parent ListViewItem from the collection.
除ListViewItem的仍然是非常的 <有/ em>的,因为在后面的功能,我能子项添加到它了!
Except the ListViewItem is still very there because later in the function I'm able to add SubItems to it again!
当然,我没有:
while(item.SubItems.Count > 0)
{
item.RemoveAt(0);
}
我?
推荐答案
不幸的是的ListView
是一个奇怪的野兽,而它的奇怪的角落之一是它的项目显示值只是一个集合,每列一个
Unfortunately ListView
is a strange beast, and one of the strange corners of it is that its items are just a collection of values to be shown, one for each column.
在名称
属性只是自动把东西放到第一列,可惜这个东西家住子项目
集合。
The Name
property just automates putting something into the first column, and unfortunately this "something" lives in the SubItems
collection.
这意味着,如果你检查子项目
集合,你会发现,它有一个元素已经,并设置项目的名称后,你会看到该项目的文本等于名称。
This means that if you inspect the SubItems
collection, you'll notice that it has one element already, and after setting the name of the item, you'll see the text of that item is equal to that name.
这是在code的<一个href="http://referencesource.microsoft.com/System.Windows.Forms/winforms/Managed/System/WinForms/ListViewItem.cs.html#bc797faa212b2976"相对=nofollow> ListViewItem.Name
属性:
This is the code for the ListViewItem.Name
property:
public string Name {
get {
if (SubItemCount == 0) {
return string.Empty;
}
else {
return subItems[0].Name;
}
}
set {
SubItems[0].Name = value;
}
}
和的<一个href="http://referencesource.microsoft.com/System.Windows.Forms/winforms/Managed/System/WinForms/ListViewItem.cs.html#c1efb233a92afdf2"相对=nofollow> ListViewSubItem.Name
属性看起来是这样的:
And the ListViewSubItem.Name
property looks like this:
public string Name {
get {
return (name == null) ? "": name;
}
set {
name = value;
if (owner != null) {
owner.UpdateSubItems(-1);
}
}
}
所以,清除子项目
收集具有清热的第一个项目的性质的后果,因为你已经发现,除了从集合删除所有其他项目
So, clearing the SubItems
collection has the consequence of clearing the properties of that first item, as you've discovered, in addition to removing every other item from the collection.
其实,什么情况是,集合被清除,但任何试图看看子项目
集合,而它是空将创建一个新的集合有一个项目,与默认属性值。在上面的例子中code,读子项目
收集将<一href="http://referencesource.microsoft.com/System.Windows.Forms/winforms/Managed/System/WinForms/ListViewItem.cs.html#c727e4759bcb95b2"相对=nofollow>自动将一个项目指定集合到内场,如果集合已不存在。
Actually, what happens is that the collection is cleared, but any attempt to look at the SubItems
collection while it is empty will create a new collection with one item, with default property values. In the above example code, reading the SubItems
collection will automagically assign a collection with one item to the internal field, if the collection isn't already there.
所以,是的,这是怎么了工程。
So yes, this is how it "works".
其实,删除除了第一个分项每次,你的循环必须是:
In fact, to remove every subitem except the first, your loop would have to be:
while (item.SubItems.Count > 1)
item.SubItems.RemoveAt(1);
这篇关于为什么SubItems.Clear()也删除Name属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!