私はそれに付けられたListViewとを持っItemActivateています。通常、私はイベントをトリガーしたものlistview.SelectedItems[0]を取得するために使用します。ListViewItem
SelectedIndexChanged今度は、アイテムが選択されるとすぐにアクティブ化できるように、サブスクライブしたいと思います。
残念ながら、これらの両方のイベントのsenderおよびeは、イベントをトリガーしたアイテムを返しません(私が知る限り!)。
回避策は、のようなフィールドをlvSelectedItem作成し、その中にアクティブなアイテムを格納することです...しかし、どうすればこれをより良い方法で行うことができますか?どういうわけか、選択したアイテムを送信者またはeventargsから直接取得できますか?
これが私のコードです:
private void lvPins_ItemActivate(object sender, EventArgs e)
{
var item = lvPins.SelectedItems[0];
var pin = item.Tag as Pin;
OnPinActivated(pin);
}
private void lvPins_SelectedIndexChanged(object sender, System.EventArgs e)
{
var item = lvPins.SelectedItems[0]; //this always cause argumentoutofrange exceotion
var pin = item.Tag as Pin;
OnPinActivated(pin);
}