ObservableCollection(ViewModel)中的MVVM ObservableCollection

时间:2014-04-07 08:06:36

标签: c# wpf mvvm observablecollection

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

例如,

我已经有了一个ObservableCollection of Products,它正确地添加和绑定到XAML。但是,现在我需要另一个包含产品项的ObservableCollection。

基本上我正在思考像

这样的视图模型
 ProductCollection[0].ProductItemCollection.Add(newProductitem);

我将如何在MVVM中解决这个问题?

由于

克里斯

1 个答案:

答案 0 :(得分:3)

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

假设您的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);
    }