获取Clicked ListView项目属性

时间:2013-03-07 14:42:50

标签: c# listview windows-8 microsoft-metro onitemclick

我有一个带有ListView的Windows 8应用程序:

<ListView x:Name="EventListView" ItemClick="EventListView_ItemClick_1" IsItemClickEnabled="True"/>

有一些Event对象(一个单独的类,其中包含字符串属性,如EventType,Description,Time等),它们是ListView的源代码:

List<Event> eventlist = new List<Event>{
    new Event(CONNECTION,   "Disconnected",         DateTime.Now.ToString(),  MONITOR,         "SAMSUNG M5",        CONNECTION_IMG,   RED),
    new Event(SYNC,         "Synchronised",         DateTime.Now.ToString(),  LAPTOP,          "ASUS X402",         SYNC_IMG,         BLUE),
    new Event(WARNING,      "Overheated!",          DateTime.Now.ToString(),  PRINTER,         "CANON MP280",       WARING_IMG,       YELLOW),
};

EventListView.ItemsSource = eventlist;

我尝试访问所点击项目的信息,但似乎没有设置:

private void EventListView_ItemClick_1(object sender, ItemClickEventArgs e)
{
    Event item = sender as Event;
    GetInfoText.Text = item.Description.ToString();
}

如何获取所点击项目的事件属性?

2 个答案:

答案 0 :(得分:4)

Event对象存储在e参数中:

private void EventListView_ItemClick_1(object sender, ItemClickEventArgs e)
{
    Event item = e.ClickedItem as Event;
    GetInfoText.Text = item.Description.ToString();
}

我相信sender参数是列表视图。

答案 1 :(得分:2)

由于您将ListView命名为 EventListView ,因此您可以执行以下操作:

private void EventListView_ItemClick_1(object sender, ItemClickEventArgs e)
{
    Event item = EventListView.SelectedItem as Event;
    GetInfoText.Text = item.Description.ToString();
}

至少,这是我的方式。