本文介绍了从 ListBox 中单击的项目中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我是 Windows Phone 新手,
I am new to Windows Phone,
我有一个包含文本块的列表框,我想从列表框中的选定项目中获取所有数据.
I have one listbox with textblocks in it, I want to fetch all data from selected item in listbox.
这是我的代码片段:
.xaml 文件
<ListBox HorizontalAlignment="Left" Name="listbox1" ItemsSource="{Binding}" Margin="9,10,0,0" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,5">
<Image HorizontalAlignment="Left" Height="100" Margin="0,15,0,0" VerticalAlignment="Top""/>
<TextBlock Text="{Binding AttractionName}" Foreground="Yellow" Margin="120,-110,0,0""/>
<TextBlock Text="Price:" Foreground="White" TextWrapping="Wrap" FontSize="30""/>
<TextBlock Text="" Foreground="Green" TextWrapping="Wrap" FontSize="40" Margin="200,-50,12,0""/>
<TextBlock Text="{Binding price}" Foreground="Green" FontSize="40""/>
<Line X1="0" X2="420" Y1="10" Y2="10" Stroke="White" VerticalAlignment="Bottom"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
.cs 文件
void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs args)
{
Debug.WriteLine(" You selected " +listbox1.SelectedItem.ToString());
}
我的控制台以这种方式显示输出:You selected Appname.Pagename.methodname
My console shows output this way: You selected Appname.Pagename.methodname
与 ListBox 绑定的类
Class which is bound to 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 (Index),您将获得一个属于您的 ItemsSource 集合的类型的项目.获得物品后,您可以随心所欲地使用它.
With SelectedItem (Index) you get an item that is Type of your ItemsSource Collection. Once you get the item you can do what you want with it.
这篇关于从 ListBox 中单击的项目中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!