问题描述
不显示ListViewItem
的常见方法是将其删除.
在我当前的项目中,与仅隐藏项目的可能性相比,这使事情变得太复杂了.
The common way to NOT display a ListViewItem
is to remove it.
I my current project, this makes things too complicated compared to the possibility of just hiding the item.
有什么方法可以隐藏ListViewItem
而不是将其删除?
Is there any way to hide a ListViewItem
instead of removing it?
到目前为止我尝试过的事情:
What I have tried so far:
-
使用
OwnerDraw=true
,DrawItem
事件没有提供任何有用的信息:Bounds
是只读的,更改Item
的属性没有用.
Using
OwnerDraw=true
, theDrawItem
event doesn't provide anything useful:Bounds
is read-only and changing properties ofItem
is useless.
继承ListView
并覆盖WndProc
是我的下一个尝试,
但是我找不到任何 LVM_???
消息很有帮助.LVM_SETITEMPOSITION
仅在View
是图标或小图标时使用.
Inherit ListView
and override WndProc
was my next attempt,
but I was not able to find any of the LVM_???
messages that helps.LVM_SETITEMPOSITION
is only used when View
is icon or small icon.
推荐答案
您可以维护一个内部列表,该列表存储一个子类,该子类是ListViewItem的子类,并且具有可见"字段.这是基本概念:
You could maintain an internal list that stores an Item that subclasses ListViewItem and has a Visible field. Here's the basic idea:
public class ListView2 : ListView {
private List<Item2> list = new List<Item2>();
public class Item2 : ListViewItem {
public bool Visible = true;
public Object Value = null;
public override string ToString() {
return (Value == null ? String.Empty : Value.ToString());
}
}
public Item2 this[int i] {
get {
return list[i];
}
}
public int Count {
get {
return list.Count;
}
}
public void Add(Object o) {
Item2 item = new Item2 { Value = o, Text = (o == null ? string.Empty : o.ToString()) };
Items.Add(item);
list.Add(item);
}
public void RefreshVisibleItems() {
var top = (Item2) this.TopItem;
Items.Clear();
int k = 0;
foreach (var o in list) {
if (o == top)
break;
if (o.Visible)
k++;
}
Items.AddRange(list.FindAll(i => i.Visible).ToArray());
if (k < Items.Count)
this.TopItem = Items[k];
}
}
var lv = new ListView2();
lv.View = View.Details;
lv.Columns.Add("Column1", 100, HorizontalAlignment.Center);
Button btn = new Button { Text = "Hide" , Dock = DockStyle.Bottom };
lv.Dock = DockStyle.Fill;
for (char c = 'A'; c <= 'Z'; c++)
lv.Add(c);
var f1 = new Form1();
f1.Controls.Add(lv);
f1.Controls.Add(btn);
btn.Click += delegate {
if (lv.Items.Count == 0) {
for (int i = 0; i < lv.Count; i++)
lv[i].Visible = true;
}
else {
lv[lv.Items.Count - 1].Visible = false;
}
lv.RefreshVisibleItems();
};
Application.Run(f1);
这篇关于隐藏ListViewItem而不是将其删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!