【发布时间】:2012-08-10 15:52:15
【问题描述】:
我正在尝试将 ObservableCollection 复制到 ObservableCollection 派生类型中,并且我想保留 CollectionChanged 事件。
到目前为止,我有:
public class PartitionCollection : ObservableCollection<AislePartition>
{
public PartitionCollection(ObservableCollection<AislePartition> other)
: base(other)
{
}
// ...
protected override void InsertItem(int index, AislePartition item)
{
// Important custom logic runs here
if (/* */)
base.InsertItem(index, item);
else
Merge(item);
}
protected void Merge(AislePartition item)
{
// ...
}
}
它可以很好地复制集合,但我也需要获取 CollectionChanged 事件。
有什么办法吗? 谢谢
编辑:
前:
后:
使用这个特定构造函数的代码:
private static void OnSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
AisleSelection aisleSelect = args.NewValue as AisleSelection;
if (aisleSelect.Partitions == null)
aisleSelect.Partitions = new PartitionCollection();
else
aisleSelect.Partitions = new PartitionCollection(aisleSelect.Partitions);
...
}
基本上我想要做的是用我的 PartitionCollection 替换 ObservableCollection,它覆盖了一些关键成员。 ObservableCollection 是从服务器以序列化的形式传给我的,所以我无法直接使用它。
【问题讨论】:
-
“克隆事件”是什么意思?
-
我的意思是我需要将所有处理程序复制到一个新事件中,并丢弃旧事件。在 C++ 中,this.CollectionChangedEvent = std::move(other.CollectionChangedEvent)。但这不是 C++ :(
标签: c# silverlight