将ListView绑定到三层深的对象列表
本文关键字:三层 对象 列表 绑定 ListView | 更新日期: 2023-09-27 18:00:18
我正在开发Windows Phone 8.1应用程序(非SL),我有以下型号:
Quiz
-- Question
-- Text
-- Options
Options 1 (name, value)
Options 2 (name, value)
Options 3 (name, value)
在我的XAML页面中,我有一个ListView。我正试图将选项列表绑定到它,就像这样:
<Page.Resources>
<DataTemplate x:Key="TemplateOptions">
<TextBlock Text="{ Binding Name }" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" Foreground="Black" FontWeight="Bold"></TextBlock>
</DataTemplate>
</Page.Resources>
<ListView Grid.Row="1" ItemsSource="{Binding Question.Options}" ItemTemplate="{StaticResource TemplateOptions}"></ListView>
但这行不通!运行应用程序时,列表为空。我做错了什么?
感谢
我让它像这样工作:
CodeBehind:
public sealed partial class MainPage : Page
{
public class Quiz
{
public Question Question { get; set; }
}
public MainPage()
{
this.InitializeComponent();
var options = new List<Option>();
options.Add(new Option { name = "foo", value = "bar" });
options.Add(new Option { name = "foo", value = "bar" });
options.Add(new Option { name = "foo", value = "bar" });
options.Add(new Option { name = "foo", value = "bar" });
var question = new Question
{
Text = "Question 1",
Options = new ObservableCollection<Option>(options)
};
this.DataContext = new Quiz { Question = question };
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
public class Question
{
public string Text { get; set; }
public ObservableCollection<Option> Options { get; set; }
}
public class Option
{
public string name { get; set; }
public string value { get; set; }
}
主页.xaml
<Page.Resources>
<DataTemplate x:Key="TemplateOptions">
<TextBlock Text="{Binding name}" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" FontWeight="Bold"></TextBlock>
</DataTemplate>
</Page.Resources>
<Grid Background="White">
<ListView ItemsSource="{Binding Question.Options}" ItemTemplate="{StaticResource TemplateOptions}"></ListView>
</Grid>