Windows phone 7 ListBox项资源绑定通过代码而不是通过XAML工作

Windows phone 7 ListBox项资源绑定通过代码而不是通过XAML工作,windows-phone-7,silverlight-3.0,binding,Windows Phone 7,Silverlight 3.0,Binding,我有这个XAML: <ListBox x:Name="MyItemsList" ItemsSource="{Binding MyItems}" SelectionChanged="ItemsList_SelectionChanged"> My ViewModel对象定义MyItems(我在设置DataContext之前初始化MyItems): 有关于为什么会发生这种情况的提示吗?谢谢。公共可观察收集我的物品;-

我有这个XAML:

<ListBox x:Name="MyItemsList"                         
     ItemsSource="{Binding MyItems}" 
     SelectionChanged="ItemsList_SelectionChanged">
My ViewModel对象定义MyItems(我在设置DataContext之前初始化MyItems):


有关于为什么会发生这种情况的提示吗?谢谢。

公共可观察收集我的物品;-字段,但您应该使用属性

具有支持字段的属性:

private ObservableCollection<Item> _myItems = new ObservableCollection<Item>();
public ObservableCollection<Item> MyItems{get{return _myItems;}}
private observetecollection\u myItems=new observetecollection();
公共ObservableCollection MyItems{get{return}MyItems;}

如果您使用whant setter,那么您应该实现INotifyPropertyChanged并调用OnPropertyChanged(“MyItems”)

好的观点;但这并没有解决我的问题。如果我在XAML中将一个随机名称作为ItemsSource属性,它也不会给我一个错误(不是在构建时,不是在运行时),这是预期的吗?现在MyItems是一个带有{get;set;}的属性,并且列表框仍然为空。我更改了我的集合,让getter返回私有集合。它仍然不起作用,我想在我的代码中的其他地方有一个问题。我将通过代码设置绑定,因为这需要花费太多的时间来解决。感谢您的帮助。VS输出窗口中没有绑定错误,但我通过查看数据绑定WP项目的示例VS项目解决了我的问题。我做了一些修改,包括在MainPage.xaml.cs构造函数中设置DataContext。谢谢
public ObservableCollection<Item> MyItems;
MyItemsList.ItemsSource = App.ViewModel.MyItems;
private ObservableCollection<Item> _myItems = new ObservableCollection<Item>();
public ObservableCollection<Item> MyItems{get{return _myItems;}}