我正在尝试使用longlistselector控件来显示People和他们可能拥有的孩子的列表。有些可能有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; }
}
任何帮助都会很棒。
答案 0 :(得分:4)
首先,你把你的课写错了:
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
集合中,则应该正确显示。