Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 合并可观测集合_C#_Wpf_Collections_Merge_Observablecollection - Fatal编程技术网

C# 合并可观测集合

C# 合并可观测集合,c#,wpf,collections,merge,observablecollection,C#,Wpf,Collections,Merge,Observablecollection,我有两个可观的集合,我需要在一个ListView控件中同时显示它们。为此,我创建了MergedCollection,它将这两个集合表示为一个ObservableCollection。通过这种方式,我可以将ListView.ItemsSource设置为我的合并集合,并列出两个集合。添加工作正常,但当我尝试删除项目时,会显示未处理的异常: An unhandled exception of type 'System.InvalidOperationException' occurred in Pre

我有两个可观的集合,我需要在一个ListView控件中同时显示它们。为此,我创建了MergedCollection,它将这两个集合表示为一个ObservableCollection。通过这种方式,我可以将ListView.ItemsSource设置为我的合并集合,并列出两个集合。添加工作正常,但当我尝试删除项目时,会显示未处理的异常:

An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
Additional information: Added item does not appear at given index '2'.
合并集合的代码如下所示:

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
}
公共类合并集合:IEnumerable,INotifyCollectionChanged
{
可观测采集节点;
可观察收集连接;
公共合并集合(ObservableCollection节点、ObservableCollection连接)
{
this.nodes=节点;
这个。连接=连接;
this.nodes.CollectionChanged+=新的NotifyCollectionChangedEventHandler(网络节点\u CollectionChanged);
this.connections.CollectionChanged+=新的NotifyCollectionChangedEventHandler(connections\u CollectionChanged);
}
无效网络节点\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
集合更改(本,e);
}
无效连接\u CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
集合更改(本,e);
}
#区域可数成员
公共IEnumerator GetEnumerator()
{
for(int i=0;i

问候

有什么原因不能使用吗

引发异常的原因是您没有将内部集合的索引转换为外部集合。您只是将完全相同的事件参数传递给外部事件(在
MergedCollection
上),这就是为什么WPF找不到索引要求它查找的项目的原因

您可以像这样使用
CompositeCollection

<ListBox>
  <ListBox.Resources>
    <CollectionViewSource x:Key="DogCollection" Source="{Binding Dogs}"/>
    <CollectionViewSource x:Key="CatCollection" Source="{Binding Cats}"/>
  </ListBox.Resources>
  <ListBox.ItemsSource>
    <CompositeCollection>
      <CollectionContainer Collection="{Binding Source={StaticResource DogCollection}}"/>
      <CollectionContainer Collection="{Binding Source={StaticResource CatCollection}}"/>
    </CompositeCollection>
   </ListBox.ItemsSource>
   <!-- ... -->
</ListBox>


有关详细信息,请参阅。

您必须偏移通知事件的索引

假设您从索引2处的第一个集合中删除了一个项。使用索引2触发集合更改事件


如果从索引2处的第二个集合中删除某个项,则事件将使用相同的索引(2)触发,但该项实际上是在第一个集合中的所有项之后枚举的。

CompositeCollection未实现INotifyCollectionChanged。@Josh:如果您遵循链接,您会看到它实现了。:-。你说得对,肯特。太好了。我遵循了链接,但我没有注意到集合实现的接口在两行上!我只看到了它!嗨,肯特,非常感谢你,这部作品集非常完美。我仍在学习.NET和WPF,但不知何故我忽略了它。谢谢。太棒了,我已经找了很长时间了。谢谢你,肯特!(我唯一的抱怨是它不是强类型的。)Kent的解决方案更好,但为了记录在案,这里是你们班最初的问题。谢谢,现在我理解了这个问题。异常消息有时对我来说有点棘手。