![](/img/trans.png)
[英]custom evaluation of columns in data grid and Binding of Observable Collection to a Column of DataGrid
[英]How to resolve null data on Observable Collection to data grid binding?
我已经在MongoLab上建立了一个数据库,该数据库被查询并解析为Model。 该模型中的集合又绑定到数据网格。 但是,当我查询数据库时,网格上显示的唯一数据是文档的对象ID。
为了调试该问题,我使用每个字段的常量数据初始化了列表,并且绑定起作用,并填充了网格中的每个字段。
然后,这使我检查了如何将数据映射到模型。
然后,我逐步完成了从服务器查询返回的Observable集合的内容。
这表明已返回所有数据,但所有模型字段均为空。 而是创建了一个客户数组,并在单独的客户对象中填充了字段。
有人知道我该如何进一步调试吗?
首先,我检查了从查询返回的集合的内容。 其中显示了空的Model字段和客户数组:
然后,我检查了的内容, customers
的客户数组(被填充):
JSON文档在MongoLab中定义,然后映射到应用程序CustomerModel中的CustomerCollection:
http://hastebin.com/ipatatoqif.pl
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));
}
}
}
这是显示在网格上的数据,目前只有ObjectID:
我不会直接在MongoDB中存储ObservableCollection<T>
。
相反,我会将List<T>
存储在MongoDB中。
为什么? ObservableCollection<T>
是WPF特定的数据结构,除非编写自定义序列化程序,否则它可能不适用于MongoDB。
如果您使用的是MVVM,则需要将存储在MongoDB中的数据与ViewModel分开。 我建议从MongoDB中检索数据,然后使用诸如AutoMapper
或ExpressMapper
的映射器将其映射到ViewModel中。
看到另一个遇到相同问题的人 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.