将ARRAY []中的多个内容加载到wp8中的列表框中

时间:2013-12-11 06:02:33

标签: c# .net windows-phone-8 listbox

我想知道如何从网址加载多个数据并在列表框中显示..

我的班级组织..

public class Organization : BaseModel
    {

        public int id {get;set;}
        public string address {get;set;}
        public string city { get; set; }
        public string contact1 {get; set;}
        public string contact2 {get; set;}
        public string country {get; set;}
        public string description {get; set;}
        public Event[] events {get; set;}//Need to load the Event data in listbox
}

我的活动[]课程..

public class Events: BaseModel
{
        public int id { get; set; }
        public DateTime event_date { get; set; }
        public string event_day { get; set; }
        public string event_location { get; set; }
        public DateTime event_time { get; set; }
}

我需要将Event[]数据加载到列表框中,任何人都可以帮我解决这个问题。

提前致谢..

2 个答案:

答案 0 :(得分:1)

如果您没有使用数据绑定,只想在列表框中显示您的Events属性,那么它将是:

foreach (var Event in Events)
{
    listBox1.Items.Add(Event);
}

//and in the ListBox, specify which property of Event to be displayed
<ListBox x:Name="listBox1" DisplayMemberPath="Name"/>

或者我可能会在问题中遗漏一些东西,因为它似乎太直接了。

更新: 要在每个列表框项中显示多个属性值,您需要指定ItemTemplate而不是简单地设置DisplayMemberPath。例如:

<ListBox x:Name="listBox1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" >
                <TextBlock Text="{Binding name}" Margin="0,0,10,0"/>
                <TextBlock Text="{Binding address}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

答案 1 :(得分:0)

而不是使用

public Event[] Events {get; set;}

使用

public List<Event> Events {get; set;}

使用

将项目添加到列表中
Events.Add(new Events
{
  id = 1,
  event_day = today
});

并使用

将其绑定到列表框
listbox.itemsource = Events;