C#WinForms ListView项目计数更改事件

WinView中的事件是否会在ListView中的项目数更改时触发? 我试过尺寸和文字 – 奇怪的是他们“sorta”工作但并不总是……

我尝试触发标签更新列表视图项的计数,因为它更改而无需手动执行一百种方法。

如果您没有使用绑定数据源,则可以在ListView控件周围创建一个包装器,并添加一个方法和一个事件,以便在将项添加到ListView集合时触发事件。

自定义ListView

public class customListView : ListView { public event EventHandler UpdateListViewCounts; public void UpdateList(string data) { // You may have to modify this depending on the // Complexity of your Items this.Items.Add(new ListViewItem(data)); CustomEventArgs e = new CustomEventArgs(Items.Count); UpdateListViewCounts(this, e); } } public class CustomEventArgs : EventArgs { private int _count; public CustomEventArgs(int count) { _count = count; } public int Count { get { return _count; } } } 

示例用法

 public partial class Form1 : Form { public Form1() { InitializeComponent(); customListView1.UpdateListViewCounts+=customListView1_UpdateListViewCounts; } private void customListView1_UpdateListViewCounts(object sender, CustomEventArgs e) { //You can check for the originating Listview if //you have multiple ones and want to implement //Multiple Labels label1.Text = e.Count.ToString(); } private void button1_Click(object sender, EventArgs e) { customListView1.UpdateList("Hello"); } }