我想通过仅从集合中推导出包含ObservableCollection的Object来引用它。如何在C#中完成?
实施例
public class Order: INotifyPropertyChanged
{
public DateTime OrderDate {get; set;}
public ObservableCollection<Item> ItemsA {get; set;}
public ObservableCollection<Item> ItemsB {get; set;}
public ObservableCollection<Item> ItemsC {get; set;}
}
代码中的参考
private void ItemsChanged(ObservableCollection<Item> items)
{
Order newOrder = items.Parent(); // <-- How can this be done?
if (newOrder.OrderDate <= new DateTime (2016, 6, 30)
{
}
}
谢谢!
答案 0 :(得分:3)
如何扩展ObservableCollection ??
public ChildObservableCollection<T> : ObservableCollection<T>
{
private Order parent { get;set;}
}
用法:
public class Order: INotifyPropertyChanged
{
public Order()
{
ItemsA = new ChildOberservableCollection<Item>();
ItemsA.parent = this;
...
}
public DateTime OrderDate {get; set;}
public ChildObservableCollection<Item> ItemsA {get; set;}
public ChildObservableCollection<Item> ItemsB {get; set;}
public ChildObservableCollection<Item> ItemsC {get; set;}
}
代码中的引用:
private void ItemsChanged(ChildObservableCollection<Item> items)
{
Order newOrder = items.parent; // done!
if (newOrder.OrderDate <= new DateTime (2016, 6, 30)
{
}
}
答案 1 :(得分:0)
我刚刚提出了一种linq方法,在某些情况下也会有所帮助:
(from order in orders
from itemA in order.ItemsA
select new { o = order.Date, i = itemA.ID }).ToList())