简体   繁体   English

如何将Observable Collection上的空数据解析为数据网格绑定?

[英]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字段和客户数组:

第1步

Then I checked the contents of the customers Customer array (which are populated): 然后,我检查了的内容, customers的客户数组(被填充):

第2步

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中检索数据,然后使用诸如AutoMapperExpressMapper的映射器将其映射到ViewModel中。

See another person who ran into the same problem . 看到另一个遇到相同问题的人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对数据网格中的列进行自定义评估,并将Observable Collection绑定到DataGrid的列 - custom evaluation of columns in data grid and Binding of Observable Collection to a Column of DataGrid 数据绑定可观察的字符串集合 - Data Binding an observable collection of strings 将数据绑定到 Windows Phone 中的可观察集合 - binding data to observable collection in windows phone 创建分组的gridview并进行分组后绑定可观察的集合数据,但是没有INotifyProperty更改影响网格吗? - Creating grouped gridview and binding Observable collection data after grouping but no INotifyProperty Change is affecting the grid? 为什么可观察集合为 xaml 数据绑定提供空值 c# wpf - Why observable collection providing null value to xaml data binding c# wpf 点击按钮后没有数据从数据网格流向 Observable Collection - No data flows from data grid to Observable Collection after hitting a button ListView不显示有关可观察的集合数据绑定的任何信息 - ListView Not showing anything about observable collection data binding 无法将数据网格绑定到我可观察的类对象集合 - Unable to bind Data Grid to my observable collection of class object 如何使用不断变化的数据更新 Observable Collection - How to update an Observable Collection with constantly changing data 将数据绑定到数据网格 - Binding Data to Data Grid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM