0

我正在尝试为从 ListView 添加或删除项目时添加一个事件。这是我到目前为止所拥有的:

protected override void OnNotifyMessage(Message m)
    {
        const int WM_ERASEBKGND = 0x14;
        const int LVM_FIRST = 0x1000;
        const int LVM_DELETEITEM = LVM_FIRST + 8;
        const int LVM_DELETEALLITEMS = LVM_FIRST + 9;
        const int LVM_INSERTITEMA = LVM_FIRST + 7;
        const int LVM_INSERTITEMW = LVM_FIRST + 77;

        //Filter out the WM_ERASEBKGND message to prevent flicker when redrawing
        if (m.Msg == WM_ERASEBKGND) return;

        base.OnNotifyMessage(m);

        if (m.Msg == LVM_DELETEITEM || m.Msg == LVM_DELETEALLITEMS)
        {
            if (ItemsRemoved == null) return;

            ItemsRemoved(null, EventArgs.Empty);
        }
        else if (m.Msg == LVM_INSERTITEMA || m.Msg == LVM_INSERTITEMW)
        {
            if (ItemsAdded == null) return;

            ItemsAdded(null, EventArgs.Empty);
        }
    }

问题是,当 ItemsRemoved 事件被触发时,ListView 的项目数没有改变。例如,当执行ListView.Items.Clear()LVM_DELETEALLITEMS 消息被处理但触发 ItemsRemoved 事件时,项目计数仍显示为 > 0。

有什么想法我在这里出错了吗?

4

0 回答 0