【发布时间】:2017-01-28 12:55:08
【问题描述】:
我的代码隐藏定义了一个带有属性和构造函数的简单类:
public class Question
{
public string[] Answers
{
get; set;
}
public int CorrectAnswerIndex
{
get; set;
}
public Question(string[] answers, int correctIndex)
{
this.Answers = answers;
this.CorrectAnswerIndex = correctIndex;
}
}
然后存在一个该类型的公共对象,它在窗口的构造函数中被初始化,如下所示:
CurrentQuestion = new Question(
new string[] { "First", "Second", "Third", "Fourth" }, 2
);
然后我有以下 XAML 以尝试打印出所述问题的所有可能答案。
<Grid Margin="150,150,150,150" DataContext="local:CurrentQuestion">
<ListBox ItemsSource="{Binding Answers}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
本地命名空间之前定义为 CLR 命名空间。
但是,我的列表完全是空的。运行时没有绑定错误。
这里发生了什么?这似乎是一个无法运行的简单示例。我觉得我错过了一些“明显的”。
【问题讨论】: