C#For-Loop未将项目添加到可观察集合

时间:2018-08-21 14:09:02

标签: c# wpf for-loop observablecollection

所以我在使用for循环将值添加到可观察的集合中时遇到问题。我正在尝试向集合中添加625个值,但是它跳过了一些值,我不知道为什么还是盲目地看到问题...

从代码中可以看到,我有两个Collections,一个已经在构造函数中预填充了值(对此没有问题),另一个正试图从地面填充新值。我有一个for循环,可以循环625次(也可以正常工作),但是以某种方式跳过了一些值,我也不知道为什么。

也许也可以说我有一个方法,它可以在CellCollection中单击某些值时进行更改,但这会在for循环之前发生,实际上与它无关,但也许值得一提。

具有前循环的方法:

     void neighborCalc()
            {
                //calculates alive neighbors and kills or sets cell alive
                for (int i = 0; i < CellCollection.Count; i++)
                {
                    int neighborAliveCounter = 0;

                    //check top
                    if (CellCollection[i].Row > 1)
                    {
                        CellModel topCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column && cell.Row == CellCollection[i].Row - 1);
                        if (topCell.IsAlive == true)
                        {
                            neighborAliveCounter = neighborAliveCounter + 1;
                        }
                    }

                    //check bottom
                    if (CellCollection[i].Row < 25)
                    {
                        CellModel bottomCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column && cell.Row == CellCollection[i].Row + 1);
                        if (bottomCell.IsAlive == true)
                        {
                            neighborAliveCounter = neighborAliveCounter + 1;
                        }
                    }

                    //check left
                    if (CellCollection[i].Column > 1)
                    {
                        CellModel leftCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column - 1 && cell.Row == CellCollection[i].Row);
                        if (leftCell.IsAlive == true)
                        {
                            neighborAliveCounter = neighborAliveCounter + 1;
                        }
                    }

                    //check right
                    if (CellCollection[i].Column < 25)
                    {
                        CellModel rightCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column + 1 && cell.Row == CellCollection[i].Row);
                        if (rightCell.IsAlive == true)
                        {
                            neighborAliveCounter = neighborAliveCounter + 1;
                        }
                    }

                    //check topleft
                    if (CellCollection[i].Column > 1 && CellCollection[i].Row > 1)
                    {
                        CellModel topLeftCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column - 1 && cell.Row == CellCollection[i].Row - 1);
                        if (topLeftCell.IsAlive == true)
                        {
                            neighborAliveCounter = neighborAliveCounter + 1;
                        }
                    }

                    //check top right cell
                    if (CellCollection[i].Row > 1 && CellCollection[i].Column < 25)
                    {
                        CellModel topRightCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column + 1 && cell.Row == CellCollection[i].Row - 1);
                        if (topRightCell.IsAlive == true)
                        {
                            neighborAliveCounter = neighborAliveCounter + 1;
                        }
                    }

                    //check bottom left cell
                    if (CellCollection[i].Row < 25 && CellCollection[i].Column > 1)
                    {
                        CellModel bottomLeftCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column - 1 && cell.Row == CellCollection[i].Row + 1);
                        if (bottomLeftCell.IsAlive == true)
                        {
                            neighborAliveCounter = neighborAliveCounter + 1;
                        }
                    }

                    //check bottom right cell
                    if (CellCollection[i].Row < 25 && CellCollection[i].Column < 25)
                    {
                        CellModel bottomRightCell = CellCollection.Single(cell => cell.Column == CellCollection[i].Column + 1 && cell.Row == CellCollection[i].Row + 1);
                        if (bottomRightCell.IsAlive == true)
                        {
                            neighborAliveCounter = neighborAliveCounter + 1;
                        }
                    }

                    //decides if to put cell alive or dead
                    if (neighborAliveCounter < 2 || neighborAliveCounter > 3)
                    {
                        tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.Transparent), IsAlive = false });
                    }

                    if (neighborAliveCounter == 3)
                    {                        
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.LightSeaGreen), IsAlive = true });
                    }
                }
            }

我不确定为什么两者都没有相同数量的值,而某些行/列却不在同一行(这是合理的,因为tempCollection某种程度上没有625个但值较少)

1 个答案:

答案 0 :(得分:4)

您在这里不处理neighborAliveCounter == 2的情况。这就是为什么计数不正确的原因。下面的代码块仅检查小于2,等于3和大于3。

//decides if to put cell alive or dead
                    if (neighborAliveCounter < 2 || neighborAliveCounter > 3)
                    {
                        tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.Transparent), IsAlive = false });
                    }

                    if (neighborAliveCounter == 3)
                    {                        
tempCollection.Add(new CellModel() { Row = CellCollection[i].Row, Column = CellCollection[i].Column, CellBackgroundColor = new SolidColorBrush(Colors.LightSeaGreen), IsAlive = true });
                    }