【发布时间】:2014-03-17 19:43:34
【问题描述】:
我要做的是:当用户单击列表框中的项目时,我想获取项目的 ID 号,这是一个属性。然后我想将此 ID 传递到另一个页面,该页面将显示相关数据。
这是我必须尝试执行此操作的代码:
private void lstCats_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Pets selectedAnimal = lstCats.SelectedItem as Pets;
NavigationService.Navigate(new Uri("/ViewPet.xaml?msg=" + selectedAnimal.ID, UriKind.Relative));
}
然后在我想要显示数据的第二页上,我有以下内容:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string msg = "";
if (NavigationContext.QueryString.TryGetValue("msg", out msg))
{
id = Convert.ToInt16(msg);
DisplayDetails();
DisplayImage();
}
}
据我所知,问题出在第一页,因为第二页在链接到其他页面时工作正常,我没有使用列表框等。
感谢任何帮助。谢谢。
编辑:我用来填充列表框的代码:
private void DisplayCats()
{
foreach (Pets temp in thisApp.pets)
{
if (temp.Category.Contains("Cat"))
{
Animal animal = new Animal() { Details = temp.Name + "\n" + temp.Category + " / " + temp.Subcategory + "\n€" + temp.Price.ToString(), ImageURI = temp.Image };
lstCats.Items.Add(animal);
}
}
}
【问题讨论】:
-
能否提供用于绑定/填充
dropdownlist的代码? -
将其添加到原始问题中,干杯
-
您将项目绑定到下拉列表为
Animal,但稍后尝试将它们转换回Pet。那是行不通的。
标签: c# windows-phone-7 windows-phone-8 observablecollection