简体   繁体   English

c#mvvm嵌套ObservableCollection CollectionChanged NewStartingIndex> 0

[英]c# mvvm nested ObservableCollection CollectionChanged NewStartingIndex >0

I'm new to stackoverflow. 我是stackoverflow的新手。 I've been looking for a solution all day long so I decided to ask here. 我一整天都在寻找解决方案所以我决定在这里问一下。

I'm working on an application which uses WPF and databinding/MVVM. 我正在开发一个使用WPF和数据绑定/ MVVM的应用程序。 I have a nested ObservableCollection which is part of a Class Program. 我有一个嵌套的ObservableCollection,它是Class Program的一部分。 Both implement INotifyPropertyChange correctly. 两者都正确实现了INotifyPropertyChange。

In my constructor I create an ObservableColletion of type Program named ListPrograms and fill it from my data service with a list of program. 在我的构造函数中,我创建了一个名为ListPrograms的Program类型的ObservableColletion,并使用程序列表从我的数据服务中填充它。 That works all well. 这很好用。 Also the binding is working correctly, in my master view I select a program and the nested list of prices is being displayed in the detail view. 此外,绑定工作正常,在我的主视图中,我选择了一个程序,并且详细视图中显示了嵌套的价格列表。 OnPropertyChange fires correctly for each property of a SalesPrice. OnPropertyChange正确触发SalesPrice的每个属性。

My requirement is that on user input the program needs to recalculate a cummulated price in every line item of the nested list. 我的要求是,在用户输入时,程序需要重新计算嵌套列表的每个行项目中的累积价格。 I have a method which does this correctly. 我有一个正确执行此操作的方法。

Im order to run that method I tried to attach a collectionchanged event. 我命令运行该方法,我试图附加一个collectionchanged事件。

`void GetPrograms()
    {        
        ListPrograms.Clear();          

        var prgms = DataService.GetProgramList(SearchStringProgram);

        ListPrograms=new ObservableCollection<Program>(prgms);

        foreach (var prg in ListPrograms)
        {
            prg.SalesPrices.CollectionChanged+=SalesPrices_CollectionChanged;
        }  `

/// ///

   void SalesPrices_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {


        if (e.NewItems != null)
            foreach (SalesPrice item in e.NewItems)
                item.PropertyChanged += SalePrice_PropertyChanged;

        if (e.OldItems != null)
            foreach (SalesPrice item in e.OldItems)
               item.PropertyChanged -= SalePrice_PropertyChanged;
    }

    void SalePrice_PropertyChanged(object sender, PropertyChangedEventArgs e)
   {
        RecalcSalePrice(CurrentProgram.SalesPrices);
    }

The problem I have is that the collection change event is only registered for new SalesPrices I add to the collections at run time. 我遇到的问题是集合更改事件仅在我在运行时添加到集合的新SalesPrices中注册。 I check the e.NewStartingIndex and it starts with the number of intial SalesPrice records. 我查看了e.NewStartingIndex,它以初始SalesPrice记录的数量开头。 (eg. the first program and 9 sales prices it its ObservableColletion and the e.NewStartingIndex has a value of 8). (例如,第一个程序和9个销售价格,它的ObservableColletion和e.NewStartingIndex的值为8)。 When I add a new record into the list at runtime the even is registering and changes are notified. 当我在运行时将新记录添加到列表中时,偶数正在注册并通知更改。

I think this is happening because I'm passing the Program object from the dataservice with a filled list of SalesPrices and the event is only registering for new items. 我认为这种情况正在发生,因为我从dataservice传递了Program对象,其中包含SalesPrices的填充列表,并且该事件仅注册新项目。

Any idea? 任何想法?

Frank 坦率

Just perform the event subscription for all existing collection items when you first parse them: 只需在首次解析时为所有现有集合项执行事件订阅:

void GetPrograms()
{        
    var prgms = DataService.GetProgramList(SearchStringProgram);

    ListPrograms=new ObservableCollection<Program>(prgms);

    foreach (var prg in ListPrograms)
    {
        prg.SalesPrices.CollectionChanged += SalesPrices_CollectionChanged;

        foreach (SalesPrice item in prg.SalesPrices)
        {
            item.PropertyChanged += SalePrice_PropertyChanged;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#在事件CollectionChanged上显示ObservableCollection的字符串 - C# Displaying String of ObservableCollection on event CollectionChanged C#:ObservableCollection-为什么没有通用的“ CollectionChanged”事件? - C#: ObservableCollection - why no generic “CollectionChanged” event? c# ObservableCollection:如何实现CollectionChanged 事件 - c# ObservableCollection: How to implement CollectionChanged event ObservableCollection CollectionChanged对WPF MVVM没有帮助 - ObservableCollection CollectionChanged not helpful in WPF MVVM C#WPF使用ObservableCollection最简化的CollectionChanged事件 - C# WPF most simplified CollectionChanged event with ObservableCollection C# - 检查ObservableCollection CollectionChanged事件是否已完成执行 - C# - Check if ObservableCollection CollectionChanged Event has finished executing 嵌套属性更改时,抛出ObservableCollection的CollectionChanged - Throw CollectionChanged of ObservableCollection when nested Property changes 如何使用 MVVM C# 添加到 ObservableCollection - How to add to ObservableCollection with MVVM C# C# WPF ListView MVVM ObservableCollection 在 ObservableCollection 内部 - C# WPF ListView MVVM ObservableCollection inside of an ObservableCollection C#MVVM:使用RelayCommand编辑ObservableCollection的SelectedItem - C# MVVM: Edit a SelectedItem of an ObservableCollection with a RelayCommand
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM