Get Clicked ListView item attributes

為{幸葍}努か 提交于 2019-12-12 15:05:06

问题


I have a Windows 8 application with a ListView:

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

There is some Event objects (a separate class with string attributes like EventType, Description, Time, etc..) that is the source of the 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;

I tried to access the info of the clicked item, but its seems to be not set:

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

How could I get the event attributes of the clicked item?


回答1:


The Event object is stored in the e parameter:

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

I believe the sender parameter is the listview.




回答2:


Since you named your ListView EventListView you can do the following:

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

At least, it is the way I do.



来源:https://stackoverflow.com/questions/15274071/get-clicked-listview-item-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!