gpt4 book ai didi

c# - 获取 ObservableCollection 的父级

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:48 27 4
gpt4 key购买 nike

我想通过仅从集合中推导它来引用包含 ObservableCollection 的对象。如何在 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)
{

}
}

谢谢!

最佳答案

如何扩展 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)
{

}
}

关于c# - 获取 ObservableCollection 的父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38452311/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com