隐藏ListViewItem而不是将其删除? [英] Hiding ListViewItem instead of removing it?

查看:95
本文介绍了隐藏ListViewItem而不是将其删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不显示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:

  1. 使用OwnerDraw=trueDrawItem事件没有提供任何有用的信息:
    Bounds是只读的,更改Item的属性没有用.

  1. Using OwnerDraw=true, the DrawItem event doesn't provide anything useful:
    Bounds is read-only and changing properties of Item is useless.

继承ListView并覆盖WndProc是我的下一个尝试,
但是我找不到任何

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 Viewis 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而不是将其删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆