繁体   English   中英

如何将项目动态添加到LongListSelector Windows Phone 8

[英]How to add items dynamically to LongListSelector Windows Phone 8

我正在尝试使用longlistselector控件来显示“人物”以及他们可能拥有的孩子的列表。 有些可能有一个孩子,有些可能会有更多。

我正在尝试使用ItemTemplate

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="KidsTemplate">
        <StackPanel Grid.Column="1" VerticalAlignment="Top">
            <TextBlock Text="{Binding KidsName}"/>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="ListTemplate">
        <StackPanel Grid.Column="1" VerticalAlignment="Top">
            <TextBlock Text="{Binding Name}"/>
            <TextBlock Text="{Binding Address}"/>
            <ListBox x:Name="PersonKids"
                     ItemTemplate="{StaticResource KidsTemplate}">
            </ListBox>
        </StackPanel>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
        <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <phone:LongListSelector x:Name="MultiList"
                            ItemsSource="{Binding Person}"
                            ItemTemplate="{StaticResource ListTemplate}">
        </phone:LongListSelector>
    </Grid>
</Grid>

尝试使用以下课程

public class PersonDetail
{
    public string Name { get; set; }
    public string Address { get; set; }
    public Kids PersonKids { get; set; }
}
public class Kids
{
    public string KidsName { get; set; }
}

在此处输入图片说明

任何帮助都会很棒。

首先,您将类写错了:

public class PersonDetail
{
    public string Name { get; set; }
    public string Address { get; set; }
    public List<Kid> PersonKids { get; set; }
}
public class Kid
{
    public string KidsName { get; set; }
}

一个人有一个孩子的清单,而不是一个。 然后为个人修复模板:

<ListBox x:Name="PersonKids"
         ItemsSource="{Binding PersonKids}"
         ItemTemplate="{StaticResource KidsTemplate}">
</ListBox>

现在,您需要为页面设置适当的DataContext 首先,将以下属性添加到页面的类中:

public ObservableCollection<PersonDetail> Persons { get; set; }

并将以下代码添加到构造函数中:

人员=新的ObservableCollection(); DataContext = this;

并且,如果将人员添加到“ Persons集合中,则应正确显示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM