我正在研究C#4.0,WPF。我有三个ListView,并且所有三个控件都具有相同的ItemContainerGenerator_ItemsChanged“事件处理程序。所以我的问题是我想从事件处理程序中找到主机ListView
。
lst1.ItemContainerGenerator.ItemsChanged += new System.Windows.Controls.Primitives.ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged);
lst2.ItemContainerGenerator.ItemsChanged += new System.Windows.Controls.Primitives.ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged);
lst3.ItemContainerGenerator.ItemsChanged += new System.Windows.Controls.Primitives.ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged);
void ItemContainerGenerator_ItemsChanged(
object sender,
System.Windows.Controls.Primitives.ItemsChangedEventArgs e)
{
//TODO: Find ListView instance.
// **REAL Problem**
// ListViewItem's Visible property has been set based on the deletion
// button click, so at one place i have to get the count of rows which
// are visible and proceed with related buttons enable/disable operation.
}
答案 0 :(得分:2)
最简单的解决方案是使用lambdas作为事件处理程序,以便您可以将ListView
实例转发到“真正的”处理程序。
lst1.ItemContainerGenerator.ItemsChanged +=
(o, e) => this.ListViewGeneratorItemsChanged(o, e, lst1);
lst2.ItemContainerGenerator.ItemsChanged +=
(o, e) => this.ListViewGeneratorItemsChanged(o, e, lst2);
lst3.ItemContainerGenerator.ItemsChanged +=
(o, e) => this.ListViewGeneratorItemsChanged(o, e, lst3);
void ListViewGeneratorItemsChanged(object sender, ItemsChangedEventArgs e, ListView listView)
{
// We have the ListView and also the ItemContainerGenerator (in sender)
}