问题
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