我正在使用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>
但这不起作用!当我运行应用程序时,列表为空。我做错了什么?
由于
答案 0 :(得分:0)
我让它像这样工作:
代码隐藏:
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; }
}
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>