3

我有一个带有 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();
}

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

4

2 回答 2

4

Event对象存储在参数e中:

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

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

于 2013-03-07T14:51:52.913 回答
2

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

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

至少,我是这样做的。

于 2013-03-07T14:59:25.857 回答