从 ListBox 中单击的项目中获取数据

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)