从列表框中单击项目获取数据 [英] Get data from clicked item in ListBox

查看:93
本文介绍了从列表框中单击项目获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Windows Phone,



我有一个列表框,在它的TextBlocks,我想在列表框来从所选项目的所有数据。



下面是我的代码片段:



.XAML文件

 <列表框的Horizo​​ntalAlignment =左NAME =ListBox1中的ItemsSource ={结合}保证金=9,10,0,0的SelectionChanged = listBox1_SelectionChanged> 
< ListBox.ItemTemplate>
<&DataTemplate的GT;
< StackPanel的保证金=0,0,0,5>
<图像的Horizo​​ntalAlignment =左HEIGHT =100保证金=0,15,0,0VerticalAlignment =顶,/>
< TextBlock的文本={结合AttractionName }前景=黄色保证金=120,-110,0,0/>
< TextBlock的文本=价格:前景=白TextWrapping =自动换行字号=30/>
< TextBlock的文本=£前景=绿色TextWrapping =自动换行字号=40保证金=200,-50,12,0/>
< TextBlock的文本={结合价格}前景=绿字号=40/>
<线X1 =0X2 =420Y1 =10 Y2 =10行程=白VerticalAlignment =底/>
< / StackPanel的>
< / DataTemplate的>
< /ListBox.ItemTemplate>
< /列表框>

.cs文件

 无效listBox1_SelectionChanged(对象发件人,SelectionChangedEventArgs参数)
{
的Debug.WriteLine(您选择了+ listbox1.SelectedItem的ToString());
}

我的控制台显示输出是这样的:您选择Appname.Pagename.methodname



这势必列表框



 公共类景点{
[JsonProperty(AttractionName)]
公共字符串AttractionName {获取;集;}
[JsonProperty(IphoneImage )]
公共字符串IphoneImage {搞定;组; }
[JsonProperty(价格)]公共字符串价格{搞定;组; }
}


解决方案

有以几种方法做到这一点:

 私人无效listBox1_SelectionChanged(对象发件人,SelectionChangedEventArgs参数)
{
如果(listBox1中。的SelectedIndex == -1)回报;

景点第一= listbox1.SelectedItem的景点;
景点秒=(景点)listBox1.Items [listBox1.SelectedIndex]
景点第三=(发件人为列表框).SelectedItem的景点;
景点第四= args.AddedItems [0]作为景点;

的Debug.WriteLine(您选择了+ first.AttractionName);
}

使用的SelectedItem(索引)你那是你的ItemsSource集合类型的项目。一旦你的项目,你可以做你想做的事情是什么。


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.

Here is my code snippet:

.xaml file

 <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 file

    void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs args)
    {
        Debug.WriteLine(" You selected " +listbox1.SelectedItem.ToString());
    }

My console shows output this way: You selected Appname.Pagename.methodname

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; } 
}

解决方案

There are few ways to do it:

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);
}

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.

这篇关于从列表框中单击项目获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆