【问题标题】:How to add items dynamically to LongListSelector Windows Phone 8如何将项目动态添加到 LongListSelector Windows Phone 8
【发布时间】:2013-12-17 16:08:59
【问题描述】:

我正在尝试使用 longlistselector 控件来显示人员和他们可能拥有的孩子的列表。有些可能有 1 个孩子,有些可能有更多。

我正在尝试使用 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; }
}

任何帮助都会很棒。

【问题讨论】:

  • 你可以使用itemscontrol

标签: c# windows-phone-8 longlistselector


【解决方案1】:

首先,你写错了你的类:

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

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

Persons = new ObservableCollection(); DataContext = this;

如果您将人员添加到 Persons 集合中,它应该会正确显示。

【讨论】:

  • 谢谢。它就像一个魅力。我知道我在做一些愚蠢的事情。
猜你喜欢
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多