Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I think Code explains my problem

C#
public ObservableCollection<Project> Project { get; set; }

 private Project _current;

    public Project Current
    {
        get { return _current; }
        set
        {
            if (_current== value)
            {
                return;
            }
            _current= value;
            NotifyOfPropertyChange(() => Current);
        }
    }

On a button Click

C#
public void SelectPump()
   {
       Project.Add(Current);
       for (var i = 0; i < Project.Count; i++)
       {
           Project[i].IdNo = i;
       }
   }

All my Project[i].IdNo is becoming 3 if my Project.Count() =3 I want it to be

C#
Project[0].IdNo = 0;
Project[1].IdNo = 1;
Project[2].IdNo = 2;
Posted

1 solution

Code example: Just try to refacoter your code based on this and see what happens.

XML
class FooObservableCollection : ObservableCollection<Foo>
{
    protected override void InsertItem(int index, Foo item)
    {
        base.Add(index, Foo);

        if (this.CollectionChanged != null)
            this.CollectionChanged(this, new NotifyCollectionChangedEventArgs (NotifyCollectionChangedAction.Add, item, index);
    }
}

var collection = new FooObservableCollection();
collection.CollectionChanged += CollectionChanged;

collection.Add(new Foo());

void CollectionChanged (object sender, NotifyCollectionChangedEventArgs e)
{
    Foo newItem = e.NewItems.OfType<Foo>().First();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900