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 中单击的项目中获取数据


- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01