私は2つのObservableCollectionを持っており、それらを一緒に1つのListViewコントロールに表示する必要があります。この目的のために、これら2つのコレクションを1つのObservableCollectionとして提示するMergedCollectionを作成しました。これにより、ListView.ItemsSourceをマージされたコレクションに設定でき、両方のコレクションがリストされます。追加すると、正常に動作しますが、私はアイテムを削除しようとすると、未処理の例外が表示されます。Merged ObservableCollection
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: Added item does not appear at given index '2'.
MergedCollectionのコードは、次のとおりです。
public class MergedCollection : IEnumerable, INotifyCollectionChanged
{
ObservableCollection<NetworkNode> nodes;
ObservableCollection<NodeConnection> connections;
public MergedCollection(ObservableCollection<NetworkNode> nodes, ObservableCollection<NodeConnection> connections)
{
this.nodes = nodes;
this.connections = connections;
this.nodes.CollectionChanged += new NotifyCollectionChangedEventHandler(NetworkNodes_CollectionChanged);
this.connections.CollectionChanged += new NotifyCollectionChangedEventHandler(Connections_CollectionChanged);
}
void NetworkNodes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
CollectionChanged(this, e);
}
void Connections_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
CollectionChanged(this, e);
}
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
for (int i = 0; i < connections.Count; i++)
{
yield return connections[i];
}
for (int i = 0; i < nodes.Count; i++)
{
yield return nodes[i];
}
}
#endregion
#region INotifyCollectionChanged Members
public event NotifyCollectionChangedEventHandler CollectionChanged;
#endregion
}
よろしく
CompositeCollectionはINotifyCollectionChangedを実装していません。 –
@ジョシュ:リンクをたどると、そのリンクが表示されます。 –
:-)あなたは正しい、ケント。それは最高です。私はリンクをたどったが、コレクションが実装しているインターフェイスが2行であることに気付かなかった!私はIListだけ見た! –