[英]How to resolve null data on Observable Collection to data grid binding?
I've set up a database on MongoLab that is queried and parsed to a Model. 我已经在MongoLab上建立了一个数据库,该数据库被查询并解析为Model。 The collection in that model is in turn bound to the data grid.
该模型中的集合又绑定到数据网格。 But when I query the database, the only data showing on the grid is the object ID for the document.
但是,当我查询数据库时,网格上显示的唯一数据是文档的对象ID。
In order to debug the issue I initialized the list with constant data for each field, and the binding works, filling each field on the grid. 为了调试该问题,我使用每个字段的常量数据初始化了列表,并且绑定起作用,并填充了网格中的每个字段。
Which then lead me to check how I'm mapping the data to the Model. 然后,这使我检查了如何将数据映射到模型。
I then stepped through the contents of the Observable collection that is returned from the server query. 然后,我逐步完成了从服务器查询返回的Observable集合的内容。
That showed that all the data is being returned, but all model fields are null. 这表明已返回所有数据,但所有模型字段均为空。 Instead an array of customers is created and fields populated within separate customer objects.
而是创建了一个客户数组,并在单独的客户对象中填充了字段。
Does anyone know how I can debug this further? 有人知道我该如何进一步调试吗?
First I checked the contents of the collection, returned from the query. 首先,我检查了从查询返回的集合的内容。 Which shows the null Model fields and the array of customers:
其中显示了空的Model字段和客户数组:
Then I checked the contents of the customers
Customer array (which are populated): 然后,我检查了的内容,
customers
的客户数组(被填充):
The document JSON is defined in MongoLab, then mapped to the CustomerCollection in the app CustomerModel: JSON文档在MongoLab中定义,然后映射到应用程序CustomerModel中的CustomerCollection:
http://hastebin.com/ipatatoqif.pl http://hastebin.com/ipatatoqif.pl
CustomerModel: CustomerModel:
public class CustomerModel : INotifyPropertyChanged
{
private ObjectId id;
private string firstName;
private string lastName;
private string email;
[BsonElement]
ObservableCollection<CustomerModel> customers { get; set; }
/// <summary>
/// This attribute is used to map the Id property to the ObjectId in the collection
/// </summary>
[BsonId]
public ObjectId Id
{
get
{
return id;
}
set
{
id = value;
}
}
[BsonElement("firstName")]
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
RaisePropertyChanged("FirstName");
}
}
[BsonElement("lastName")]
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
RaisePropertyChanged("LastName");
}
}
[BsonElement("email")]
public string Email
{
get
{
return email;
}
set
{
email = value;
RaisePropertyChanged("Email");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
This is the data that shows on the grid, only the ObjectID at present: 这是显示在网格上的数据,目前只有ObjectID:
I wouldn't store an ObservableCollection<T>
directly in MongoDB. 我不会直接在MongoDB中存储
ObservableCollection<T>
。
Instead, I would store a List<T>
in MongoDB. 相反,我会将
List<T>
存储在MongoDB中。
Why? 为什么? An
ObservableCollection<T>
is a WPF-specific data structure, and probably won't work with MongoDB unless you write a custom serializer . ObservableCollection<T>
是WPF特定的数据结构,除非编写自定义序列化程序,否则它可能不适用于MongoDB。
If you are using MVVM, you need to separate the data stored in MongoDB from the ViewModel. 如果您使用的是MVVM,则需要将存储在MongoDB中的数据与ViewModel分开。 I would recommend retrieving the data from MongoDB, then mapping this into your ViewModel using a mapper such as
AutoMapper
or ExpressMapper
. 我建议从MongoDB中检索数据,然后使用诸如
AutoMapper
或ExpressMapper
的映射器将其映射到ViewModel中。
See another person who ran into the same problem . 看到另一个遇到相同问题的人 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.