从ListBox中单击的项目获取数据
我是Windows Phone的新手,
我有一个带有文本块的列表框,我想从列表框中的选定项目中获取所有数据。
这是我的代码片段:
.xaml文件
.cs文件
void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs args) { Debug.WriteLine(" You selected " +listbox1.SelectedItem.ToString()); }
我的控制台以这种方式显示输出: You selected Appname.Pagename.methodname
绑定到ListBox的类
public class Attractions { [JsonProperty("AttractionName")] public string AttractionName { get; set; } [JsonProperty("IphoneImage")] public string IphoneImage { get; set; } [JsonProperty("price")] public string price { get; set; } }
有几种方法可以做到:
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs args) { if (listBox1.SelectedIndex == -1) return; Attractions first = listbox1.SelectedItem as Attractions ; Attractions second = (Attractions)listBox1.Items[listBox1.SelectedIndex]; Attractions third = (sender as ListBox).SelectedItem as Attractions; Attractions fourth = args.AddedItems[0] as Attractions; Debug.WriteLine(" You selected " + first.AttractionName); }
使用SelectedItem(索引),您将获得一个项目,即ItemsSource Collection的类型。 获得物品后,您可以随心所欲地完成任务。
在SelectionChange Event
内部放置以下代码
Attractions lbi = (sender as ListBox).SelectedItem as Attractions;
您可以使用访问该类的属性
lbi.price
写下这段代码
这可以帮到你
void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs args) { Debug.WriteLine(" You selected " +listBox1.Items[listBox1.SelectedIndex].ToString ()); }