首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVVM ObservableCollection inside a ObservableCollection (ViewModel)

MVVM ObservableCollection inside a ObservableCollection (ViewModel)
EN

Stack Overflow用户
提问于 2014-04-07 08:06:36
回答 1查看 2.7K关注 0票数 1

我只是想知道我怎样才能拥有一个父集合的子集合?

例如,

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

基本上,在视图模型中,我认为

代码语言:javascript
复制
 ProductCollection[0].ProductItemCollection.Add(newProductitem);

在MVVM中我该怎么做呢?

谢谢

克里斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-07 08:33:25

不知道这是不是你要找的但是..。

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

代码语言:javascript
复制
<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>

您已经声明了

代码语言:javascript
复制
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中第二个网格的项目源,如下所示

代码语言:javascript
复制
    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");
        }
    }

最后,添加一些示例数据。

代码语言:javascript
复制
    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);
    }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22906799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档