![](/img/trans.png)
[英]Windows Phone 7.1: How to add & delete items from LongListSelector control?
[英]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.