您可以将您的绑定ListView
到一个对象,而不仅仅是一个字符串。如果这样做,当单击一个项目时,您将取回该对象。在您的情况下,您可以绑定到Bowler
对象,我假设它有一个ID
andName
字段:
public class Bowler
{
public int Id { get; set; }
public string Name { get; set; }
}
在您的 XAML 中,您必须告诉ListView
您要绑定该Name
字段。这是一个简单的,每个项目ListView
都有一个:TextBlock
<ListView x:Name="itemListView" ItemsSource="{Binding}"
IsItemClickEnabled="True" ItemClick="itemListView_ItemClick">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在上面,TextBlock
将显示该Name
字段。
要将项目加载到列表中,您可以将上述方法更改为如下所示:
// Unrelated, but generally you want to use "async Task"
// in the method signature. "async void" should only be used by
// event handlers, such as a click event.
private async Task getBowlers()
{
SQLiteAsyncConnection conn = new SQLiteAsyncConnection(BOWLERS_DATABASE);
var query = conn.Table<Bowler>();
var result = await query.ToListAsync();
// set page's data context to bowler collection
this.DataContext = result;
}
现在,当单击一个项目时,您将返回相关Bowler
对象:
private void itemListView_ItemClick(object sender, ItemClickEventArgs e)
{
Bowler bowler = (Bowler)e.ClickedItem;
// do something with bowler...
}
更多信息:Windows 8 Metro App ListView 绑定和编辑