MVVM ObservableCollection inside a ObservableCollection (Vie

本文关键字:ObservableCollection Vie inside MVVM | 更新日期: 2023-09-27 17:57:09

我只是想知道如何拥有父集合的子集合?

例如

我已经有一个可观察的产品集合,它正在正确添加和绑定到 XAML。但是,现在我需要另一个包含产品项的可观察集合。

基本上我在视图模型中思考有类似的东西

 ProductCollection[0].ProductItemCollection.Add(newProductitem);

我将如何在 MVVM 中执行此操作?

谢谢

克里斯

MVVM ObservableCollection inside a ObservableCollection (Vie

不确定这是否是您要找的,但是...

假设您的 xaml 中有两个网格。第一个显示您的产品,第二个显示所选产品的项目。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Height="350"
    Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="173*" />
        <RowDefinition Height="147*" />
    </Grid.RowDefinitions>
    <DataGrid ItemsSource="{Binding ProductsCollection}"
              SelectedItem="{Binding SelectedProduct}"
              Margin="10">
    </DataGrid>
    <DataGrid ItemsSource="{Binding ProductItemsCollection}"
              Margin="10"
              Grid.Row="1">
    </DataGrid>
</Grid>

您已声明了类

public class Product
{
    public Product()
    {
        ItemsCollection = new ObservableCollection<Item>();
    }
    public int ID { get; set; }
    public string Name { get; set; }
    public ObservableCollection<Item> ItemsCollection { get; set; }
}
public class Item
{
    public int ID { get; set; }
    public DateTime Date { get; set; }
}

从第一个网格中选择产品将更新 VM 中第二个网格的项源,如下所示

    private ObservableCollection<Product> _ProductsCollection = new ObservableCollection<Product>();
    public ObservableCollection<Product> ProductsCollection
    {
        get{return _ProductsCollection;}
        set
        {
            _ProductsCollection = value;
            OnPropertyChanged("ProductsCollection");
        }
    }
    private ObservableCollection<Item> _ProductItemsCollection;
    public ObservableCollection<Item> ProductItemsCollection
    {
        get {return _ProductItemsCollection; }
        set
        {
            _ProductItemsCollection = value;
            OnPropertyChanged("ProductItemsCollection");
        }
    }
    private Product _SelectedProduct = null;
    public Product SelectedProduct
    {
        get {return _SelectedProduct;}
        set
        {
            _SelectedProduct = value;
            ProductItemsCollection = _SelectedProduct.ItemsCollection;
            OnPropertyChanged("SelectedProduct");
        }
    }

最后,添加一些示例数据

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        Product product1 = new Product() { ID = 1, Name = "Product1" };
        product1.ItemsCollection.Add(new Item() { ID = 1, Date = DateTime.Now});
        product1.ItemsCollection.Add(new Item() { ID = 2, Date = DateTime.Now.AddDays(-1) });
        Product product2 = new Product() { ID = 2, Name = "Product2" };
        product2.ItemsCollection.Add(new Item() { ID = 3, Date = DateTime.Now });
        product2.ItemsCollection.Add(new Item() { ID = 4, Date = DateTime.Now.AddDays(-2) });
        ProductsCollection.Add(product1);
        ProductsCollection.Add(product2);
    }