如何在 GridView 中对两个表进行数据绑定?

How to databind two tables in GridView?

基本上,我需要加入两个 ObservableCollections,例如 ProductBrandInfo,其中 Product 模型有一个非官方定义的 foreign keyBrandInfo。这些模型正在使用 Sqlite-Net 进行数据持久化。

我想在一个GridView中显示两者的组合,这将作为以后在其他Xaml屏幕上进行数据操作的基础。

我当前的设置通过 viewModel class 绑定数据,通过 Product 模型获取数据。 viewModel 有一个 ProductObservableCollection 实例,并且可以正常工作。

根据我的阅读,我可能需要使用 CompositeCollection 或者可能创建一个模型来表示 ProductBrandInfojoin。考虑到 Sqlite-Net 不支持像 Entity Framework 和其他 ORM's 这样的外键,我不太确定如何使用 CompositeCollection。我也不确定对这两种模型进行非数据库存储组合是否是个好主意。任何建议或片段都可以帮助我解决问题![​​=35=]

提前致谢!

一些代码:

MainWindowViewModel.cs

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using StoreFront.Models;
using StoreFront.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace StoreFront.ViewModels {
    public class MainWindowViewModel : ViewModelBase,IMainViewModel {

        private IDataService dataService;

        IDialogService dialogService;
        IStorageService storageService;

        public MainViewModel(IDataService dataService, IStorageService storageService) {
            this.dataService = dataService;
            this.storageService = storageService; 

            RefreshAsync(); //Basically fills the product ObservableCollection with data.
        }

        private ObservableCollection<Product> product = new ObservableCollection<Product>();
        public ObservableCollection<Product> Product {
            get {
                return product;
            }
        }

}

Product.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLite;

namespace StoreFront.Models {
    public class Product {

        [PrimaryKey, AutoIncrement]
        public int Id { get; set;}

        public string ItemName { get; set;}

        public double Price { get; set;}

        public int BrandInfoFK { get; set;}    
    }
}

BrandInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLite;

namespace StoreFront.Models {
    public class BrandInfo {

        [PrimaryKey, AutoIncrement]
        public int Id { get; set;}

        public string BrandName { get; set;}

        public string Description { get; set;} 
    }
}

编辑 我最初的回答完全是基于对问题的误解。

由于我对SQLite不熟悉,所以我不确定你的Product和BrandInfo模型是通过代码生成还是手动创建的。如果手动创建,您能否将 BrandInfo 属性添加到 Product?如果可能,您可以使用 linq 加入您的产品和品牌信息模型集合,以创建一个您可以在 GridView 中绑定的集合:

void Main()
{
    List<Product> products = new List<Product>() { new Product() {Id = 1, ItemName="Item1", BrandInfoFK=1}, new Product() {Id=2, ItemName="Item2", BrandInfoFK=2}};
    List<BrandInfo> brands = new List<BrandInfo>() { new BrandInfo() { Id = 1, BrandName="One"}, new BrandInfo() {Id = 2, BrandName="Two"}};


    List<Product> joinedProducts = products.Join(brands, p => p.BrandInfoFK, b => b.Id, (p, b) => new Product ()
    {
        Id = p.Id,
        ItemName = p.ItemName,
        Price = p.Price,
        BrandInfoFK = p.BrandInfoFK,
        BrandName = b.BrandName,
        BrandDescription = b.Description
    }).ToList();
}


public class Product {

    public int Id { get; set;}

    public string ItemName { get; set;}

    public double Price { get; set;}

    public int BrandInfoFK { get; set;}    

    // Brand properties
    public string BrandName {get; set;}

    public string BrandDescription {get; set;}
}

public class BrandInfo {
    public int Id { get; set;}

    public string BrandName { get; set;}

    public string Description { get; set;} 
}

我在这里使用 List,但您可以轻松适应 ObservableCollection。此外,如果您 cannot/don 不想将 BrandInfo 属性添加到您的 Product 模型中,您可以创建一个 class 从添加这些属性的 Product 派生的?