Modify the ObservableCollection element

I have added several items to the ObservableCollection and now I want to change one of them, for example:

_MyCollection[num].Data1 = someText;

      

As an example, according to the code below, the intent is: _MyCollection[5].Type = changedText;

_MyCollection.Add(new MyData
{
    Boundary = Text1,
    Type = Text2,
    Option = Text3
});

      

How can i do this?

+3


source to share


2 answers


I think you just want to see the changes right? This has nothing to do with the ObservableCollection, but with your object MyData

. It should implement INotifyPropertyChange

- if you do, you will see the changes you made.



public class MyData : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string type;
    public string Type
    {
      get { return type; }
      set
      {
         if (value != type)
         {
            type = value;
            NotifyPropertyChanged("Type");
         }
      }
    }

    // ... more properties

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

      

+5


source


This will lead to an event CollectionChanged

:



MyData temp = _MyCollection[index];
temp.Type = changedText;
_MyCollection.SetItem(index, temp);

      

+1


source







All Articles