如何填充LongListSelector [英] How to populate LongListSelector

查看:147
本文介绍了如何填充LongListSelector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始接触C#的Windows Phone 8的开发,我试图写它采用了LongListSelector的应用程序。该应用程序会显示车站名称的长长的名单。

I'm starting out with C# Windows Phone 8 development and I am trying to write an app which uses a LongListSelector. The app will show a long list of train station names.

我一直在寻找一些样品在网上,其中包括MSDN PeopleHub和PhotoHub样品,但他们似乎很复杂。我接过PeopleHub样品,并在其砍死一点,直到我得到了它在LongListSelector显示电台列表,但我没能找到已经在列表中选择该项目。传递到另一页的时候,而不是哪个项目已经在列表中被挑选的SelectedItem属性刚刚返回应用程序的名称。

I have been looking at some of the samples online, including the MSDN PeopleHub, and PhotoHub samples but they seem very complex. I took the PeopleHub sample and hacked at it a bit until I got it to display a list of stations in a LongListSelector but I wasn't able to find out which item had been selected in the list. The SelectedItem property was just returning the app name when passed to another page and not which item had been picked in the list.

我想我需要怎样的一些项目添加到LongListSelector,然后找到并选定项目传递到另一个页面的基本示例。我不完全明白是怎么控制的工作,如果你要使用某种形式的数据绑定与LongListSelector填充它,还是它就像简单的东西:

I think I need a basic example of how to add some items to a LongListSelector and then find and pass the selected item to another page. I don't fully understand how the control works, if you have to use some sort of DataBinding with the LongListSelector to populate it or whether it's something simpler like:

LongListSelectorThing.add("trainstationA");
LongListSelectorThing.add("trainstationB");
LongListSelectorThing.add("trainstationC");

有人可以给我一些简单的基本指针,以如何填充这种控制,并找出哪些项目的用户选择?当我说,他们选择的项目,当LongListSelector电网出现,他们点击A为例,然后它显示A开头的事情的清单,然后他们点击trainstationA,我希望能够检测到他们'已经挑trainstationA和传递信息到另一个页面,所以我可以显示有关它的更多信息。

Can someone give me some simple basic pointers as to how to populate this control and find out which item the user selects? When I say which item they select, when the LongListSelector grid appears, they click on A for example, and it then shows a list of things beginning with A and they then click on trainstationA, I'd want to be able to detect they've picked trainstationA and pass that info to another page so I can display further information about it.

很抱歉,如果这似乎基本,我是很新的这一点。

Sorry for if this seems basic, I'm quite new to this.

谢谢!

推荐答案

下面是一个基本的例子,这应有助于你理解:
首先在你的页面(XAML文件)可以定义控制LongListSelector(LLS):

Here is a basic example which should help you understand: First in your Page (xaml file) you define the control LongListSelector (LLS):

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <phone:LongListSelector Name="myLLS" Margin="0">
        <phone:LongListSelector.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </phone:LongListSelector.ItemTemplate>
    </phone:LongListSelector>
</Grid>

您还声明其项目将如何样子。它可以是任何的UIElement - 一个按钮,图像,网格等等。在code上面我宣布我的项目将是一个TextBlock哪些内容(文本)我已经绑定到一个属性名称。我还给出了一个LLS名,可以参考我在以后。

You also declare how its Items will look like. It can be any UIElement - a button, Image, Grid and so on. In the code above I declared that my Item would be a TextBlock which content (text) I've bound to a property 'Name'. I've also given the LLS a name, that I can refer to it later.

在Page.cs code你填充LLS。让我们创建简单的站班,并填充LLS:

In Page.cs code you populate the LLS. Lets create the simple Station class, and populate LLS:

public class Station
{
  private string _stationName;

  public string Name
  {
     get { return _stationName; }
     set { _stationName = value; }
  }

  public Station(string station)
  {
     this.Name = station;
  }
}

public partial class MainPage : PhoneApplicationPage
{
  ObservableCollection<Station> trainStations = new ObservableCollection<Station>();

  public MainPage()
  {
     InitializeComponent();

     myLLS.ItemsSource = trainStations;

     trainStations.Add(new Station("Germany"));
     trainStations.Add(new Station("France"));
     trainStations.Add(new Station("Italy"));
  }
}

什么是重要的:


  • 看在我站班上有被称为房地产名称 - 这是到了TextBlock的内容势必一个

  • 我创建的ObservableCollection这是我站的集合 - 这是类似于列表,但是当新的项目被添加或删除PropertyChanged事件引发,因此您LongListSelector可以在添加新站自动更新。

  • 我已经指派创建集合myLLS.ItemsSource - 这意味着创建LLS将与项目(在XAML作为描述的DataTemplate)来填充和本项的来源是集

希望这有助于。快乐编码。

Hope this helps. Happy coding.

这篇关于如何填充LongListSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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