C# 将ListView绑定到三层深的对象列表
我正在使用Windows Phone 8.1应用程序(非SL),我有以下型号:C# 将ListView绑定到三层深的对象列表,c#,xaml,listview,windows-phone-8.1,C#,Xaml,Listview,Windows Phone 8.1,我正在使用Windows Phone 8.1应用程序(非SL),我有以下型号: Quiz -- Question -- Text -- Options Options 1 (name, value) Options 2 (name, value) Options 3 (name, value) 在我的XAML页面中,我有一个ListView。我试图将选项列表绑定到它,如下所示: &l
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>
但这不管用!当我运行应用程序时,列表为空。我做错了什么
谢谢我让它这样工作: 代码隐藏:
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; }
}
公共密封部分类主页面:第页
{
公开课测验
{
公共问题{get;set;}
}
公共主页()
{
this.InitializeComponent();
var options=新列表();
添加(新选项{name=“foo”,value=“bar”});
添加(新选项{name=“foo”,value=“bar”});
添加(新选项{name=“foo”,value=“bar”});
添加(新选项{name=“foo”,value=“bar”});
var问题=新问题
{
Text=“问题1”,
选项=新的ObservableCollection(选项)
};
this.DataContext=新测验{Question=Question};
}
///
///当此页面即将显示在框架中时调用。
///
///描述如何到达此页面的事件数据。
///此参数通常用于配置页面。
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
}
}
公开课问题
{
公共字符串文本{get;set;}
公共ObservableCollection选项{get;set;}
}
公共类选项
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
}
MainPage.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>
您的ItemsSource似乎绑定到了错误的属性。它应该绑定到您的问题集合中。@JamesPack我对代码进行了更正。我只有一个问题
在测验
下,还有一组选项
在问题
下,您在输出窗口中是否有任何绑定错误?你的页面背景是什么颜色?我也很喜欢,但不是3层,只有2层。我怎样才能让它像我一样工作<代码>测验问题
列表
我一定是误解了你的提问。对不起,你的问题说有两层深。列表有两层深,但是列表中的对象有三层深,如果这有意义的话?对不起,这可能是我的错。我会更新标题。