在 Windows Phone 中的 XAML 中绑定 Observable 集合
我在简单的 Windows Phone 7 页面中以声明方式设置 PivotItem 中包含的 ListBox 的 ItemsSource 时遇到问题。我可以在后面的代码中成功设置 ItemsSource 。
以下是包含我想要绑定的 ObservableCollection 的类的片段:
sealed class Database : INotifyPropertyChanged
{
//Declare Instance
private static readonly Database instance = new Database();
//Private Constructor
private Database() { }
//The entry point into this Database
public static Database Instance
{
get
{
return instance;
}
}
#region Collections corresponding with database tables
public ObservableCollection<Category> Categories { get; set; }
public ObservableCollection<CategoryType> CategoryTypes { get; set; }
这是我的 XAML 示例:
<ListBox x:Name="CategoriesListBox" Margin="0,0,-12,0" ItemsSource="{Binding Categories}" DisplayMemberPath="Name" />
在我的页面中,我尝试按如下方式设置数据上下文:
this.DataContext = Database.Instance;
但是,除非我显式设置,否则绑定不起作用代码中的 ItemsSource 如下:
CategoriesListBox.ItemsSource = Database.Instance.Categories;
我知道我应该能够以声明方式完成所有这些操作,但是我尝试了许多不同的以声明方式设置 ItemsSource 的方法(除了上面详细介绍的方法之外),但没有任何效果。
有人可以帮我吗?
进一步信息:运行时的输出窗口显示以下内容: System.Windows.Data 错误:无法从 ' 获取 'Categories' 值(类型 'System.Collections.ObjectModel.ObservableCollection`1[BTT.PinPointTime.Entities.Category]') BTT.PinPointTime.WinPhone.Database”(类型“BTT.PinPointTime.WinPhone.Database”)。 BindingExpression: Path='类别' DataItem='BTT.PinPointTime.WinPhone.Database' (HashCode=99825759);目标元素是“System.Windows.Controls.ListBox”(名称=“CategoriesListBox”);目标属性是“ItemsSource”(类型“System.Collections.IEnumerable”).. System.MethodAccessException:尝试访问该方法失败:BTT.PinPointTime.WinPhone.Database.get_Categories() 在 System.Reflection.RuntimeMethodInfo.InternalInvoke(对象 obj,BindingFlags invokeAttr
I am having trouble with declaratively setting the ItemsSource of a ListBox contained within a PivotItem in a simple Windows Phone 7 page. I can successfully set the ItemsSource in code behind.
Here is a snippet of the class that contains the ObservableCollection that I want to bind to:
sealed class Database : INotifyPropertyChanged
{
//Declare Instance
private static readonly Database instance = new Database();
//Private Constructor
private Database() { }
//The entry point into this Database
public static Database Instance
{
get
{
return instance;
}
}
#region Collections corresponding with database tables
public ObservableCollection<Category> Categories { get; set; }
public ObservableCollection<CategoryType> CategoryTypes { get; set; }
And here is a sample of my XAML:
<ListBox x:Name="CategoriesListBox" Margin="0,0,-12,0" ItemsSource="{Binding Categories}" DisplayMemberPath="Name" />
In my page I have tried setting the data context as follows:
this.DataContext = Database.Instance;
However the binding does not work unless I explicitly set the ItemsSource in code as follows:
CategoriesListBox.ItemsSource = Database.Instance.Categories;
I know that I should be able to do this all declaratively, however I have tried many different ways of setting the ItemsSource declaratively (in addition to what I have detailed above) and none work.
Can someone help me out?
Further info: The output windows at runtime shows the following: System.Windows.Data Error: Cannot get 'Categories' value (type 'System.Collections.ObjectModel.ObservableCollection`1[BTT.PinPointTime.Entities.Category]') from 'BTT.PinPointTime.WinPhone.Database' (type 'BTT.PinPointTime.WinPhone.Database'). BindingExpression: Path='Categories' DataItem='BTT.PinPointTime.WinPhone.Database' (HashCode=99825759); target element is 'System.Windows.Controls.ListBox' (Name='CategoriesListBox'); target property is 'ItemsSource' (type 'System.Collections.IEnumerable').. System.MethodAccessException: Attempt to access the method failed: BTT.PinPointTime.WinPhone.Database.get_Categories()
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现问题与我的数据库类的访问级别有关。当我将其从“密封”更改为“公共密封”时,数据绑定起作用了。
I found out that the problem was related to the access level of my Database class. When I changed it from "sealed" to "public sealed" the databinding worked.
嗯,我看到您正在实现 INotifyPropertyChanged,但没有使用它。您应该像这样在 get 中添加
NotifyPropertyChanged("Categories");
:当您想要将数据添加到类别集合时,请使用属性而不是成员。它在我的代码中有效,希望这有帮助。
Hmm I see that you are implementing
INotifyPropertyChanged
, but not using it. You should addNotifyPropertyChanged("Categories");
in the get like so:when you want to add data to the category collection, use the property and not the member. it worked in my code, hope this helps.
尝试在构造函数代码中实例化类别。
现在您的绑定将正确识别该集合。
Try instantiating the Categories in your Constructor code.
Now your binding will know the collection properly.