Get data from clicked item in ListBox(从 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 中单击的项目中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 ListBox 中单击的项目中获取数据


基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01