在C#Windows窗体应用程序中;是否有一个ListView控件的事件处理程序,当项目添加到listview项时会被触发?
答案 0 :(得分:9)
您无需编辑其他来源!
好的:从ListView更改为myListView
很久以前 - 但是我搜索没有带有自己的ItemAdd-Function的工具的解决方案! 最好的方法...使用WndProc函数。
消息:LVM_INSERTITEM
http://msdn.microsoft.com/en-us/library/windows/desktop/bb761107%28v=vs.85%29.aspx
//COMMCTRL.H
#define LVM_FIRST 0x1000 // ListView messages
#define LVM_INSERTITEMA (LVM_FIRST + 7)
#define LVM_INSERTITEMW (LVM_FIRST + 77)
//edit itemremove (LVM_DELETEITEM)
#define LVM_DELETEITEM (LVM_FIRST + 8)
C# - 实施
class myListView : ListView {
protected override void WndProc(ref Message m){
base.WndProc(ref m);
switch (m.Msg){
case 0x1007: //ListViewItemAdd-A
System.Diagnostics.Debug.WriteLine("Item added (A)");
break;
case 0x104D: //ListViewItemAdd-W
System.Diagnostics.Debug.WriteLine("Item added (W)");
break;
//edit for itemremove
case 0x1008:
System.Diagnostics.Debug.WriteLine("Item removed");
break;
case 0x1009:
System.Diagnostics.Debug.WriteLine("Item removed (All)");
break;
default:
break;
}
}
}
现在您可以激活自己的ItemAddedEvent。我希望能帮助其他有同样问题的人。
gegards raiserle
(编辑:请投票;))
答案 1 :(得分:4)
答案 2 :(得分:4)
没有事件可以做到这一点。 但是你总是可以创建自己的列表框:
public class MyListView : ListView
{
public void AddItem(ListViewItem item)
{
Items.Add(item);
if (ItemAdded != null)
ItemAdded.Invoke(this, new ItemsAddedArgs(item));
}
public EventHandler<ItemsAddedArgs> ItemAdded;
}
public class ItemsAddedArgs : EventArgs
{
public ItemsAddedArgs(ListViewItem item)
{
Item = item;
}
public object Item { get; set; }
}
答案 3 :(得分:1)
另一种方法是将项目保存在ObservableCollection类的实例中,将ListView.ItemsSource设置为该集合并订阅ObservableCollection.CollectionChanged事件。
答案 4 :(得分:0)
框架不定义ItemAdded
等事件。但是,请务必访问此解决方法:An Observer Pattern and an Extended ListView Event Model。例如,在那里定义了以下事件:
public event ListViewItemDelegate ItemAdded;
public event ListViewItemRangeDelegate ItemRangeAdded;
public event ListViewRemoveDelegate ItemRemoved;
public event ListViewRemoveAtDelegate ItemRemovedAt;
答案 5 :(得分:0)
public class CustomListView : ListView
{
private const int LVM_FIRST = 0x1000;
private const int LVM_INSERTITEMA = LVM_FIRST + 7;
private const int LVM_INSERTITEMW = LVM_FIRST + 77;
private const int LVM_DELETEITEM = LVM_FIRST + 8;
private const int LVM_DELETEITEMALL = LVM_FIRST + 9;
private CustomListViewItemOffsetSorter m_sorter;
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Timer m_timer = null;
public CustomListView()
: base()
{
this.components = new System.ComponentModel.Container();
this.m_timer = new Timer(this.components);
this.m_timer.Interval = 1;
this.m_timer.Tick += (s, e) =>
{
this.m_timer.Enabled = false;
if (ItemCountChanged != null)
{
ItemCountChanged.Invoke(this, EventArgs.Empty);
}
};
}
public event EventHandler ItemCountChanged;
private void OnItemCountChanged()
{
this.m_timer.Enabled = false;
this.m_timer.Enabled = true;
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case LVM_INSERTITEMA:
case LVM_INSERTITEMW: //ListViewItemAdd-W
case LVM_DELETEITEM:
case LVM_DELETEITEMALL:
this.OnItemCountChanged();
break;
default:
break;
}
}
}
//--------------------------------------------------------//
CustomListView listView = new CustomListView();
listView.ItemCountChanged += new EventHandler(listView_ItemCountChanged);
private void listView_ItemCountChanged(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("ItemCountChanged: {0}", listView.Items.Count);
}