我有一个Windows Phone应用程序,我正在尝试将ObservableCollection绑定到Listbox以列出字符串。然而,当我运行应用程序时,没有列出任何内容,我看不出哪里出了问题。
XAML:
<ListBox ItemsSource="{Binding EventList}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Foreground="Black" FontSize="20"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
代码背后:
private ObservableCollection<String> eventList = new ObservableCollection<String>();
public ObservableCollection<String> EventList
{
get { return eventList; }
}
public MainPage()
{
eventList.Add("Event One");
eventList.Add("Event Two");
eventList.Add("Event Three");
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
是否将主页面的DataContext设置为具有EventList属性的应用程序类?在get访问器上设置断点,并检查是否正在调用它。如果没有,则需要设置DataContext。