0

I am struggling with DataBindings. I have a ListView (Displays Correctly) With items and I need to be able to edit the items in the list.

I select an item which opens a modal (works fine) with the information of the selected item (works fine). When I click save, the item is not updated - The display is not updated, but if I select the item again, the data is correctly held.

I have the following object:

 public class Investigation : IDisposable
 {
     public List<InjuredPerson> InjuredPersonnel { get; set; }
     ...
 }

My ViewModel is like this:

public class InvestigateUtilityDamagesViewModel : INotifyPropertyChanged
{
    Investigation investigation;
    private InvestigateDamages damage;

    public event PropertyChangedEventHandler PropertyChanged;

    public InvestigateUtilityDamagesViewModel(InvestigateDamages damage)
    {
        this.damage = damage;
        Investigation = new Investigation();
        Investigation.DamageID = damage.DamageID;
        Investigation.InjuredPersonnel = damage.DamageDetails.InjuredPersonnel;
    }

    public Investigation Investigation
    {
        get { return investigation; }
        set
        {
            if (investigation == value)
            {
                return;
            }
            investigation = value;
            OnPropertyChanged("Investigation");
        }
    }

    void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
        SaveInvestigation();
    }
}

The XAML:

 <ListView ItemsSource="{Binding Investigation.InjuredPersonnel, Mode=TwoWay}">
...

The page which updates Information sends a Message like so: (works fine)

MessagingCenter.Send<EditInjuredPerson, InjuredPerson>(this, "InjuredPersonEdited", _injuredPerson);

And the receiving side (works fine)

private void SaveInjuredPerson(EditInjuredPerson sender, InjuredPerson InjuredPerson)
    {
        var Injured = this.FindByName<ListView>("listInjuries").SelectedItem as InjuredPerson;
        if (Injured != null)

        {
            Injured.Name = InjuredPerson.Name;
            Injured.Position = InjuredPerson.Position;
            Injured.ContactNumber = InjuredPerson.ContactNumber;
            Injured.Injury = InjuredPerson.Injury;
            Injured.NextOfKinName = InjuredPerson.NextOfKinName;
            Injured.NextOfKinNumber = InjuredPerson.NextOfKinNumber;
        }
    }
Abrar Ali
  • 115
  • 2
  • 14

1 Answers1

0

The InjuredPersonnel list needs the OnProprertyChanged event raised on it, not the Investigation (or in addition to).

Alternatively, convert the List<> to ObservableCollection<>.

public List<InjuredPerson> InjuredPersonnel { get; set; }

becomes

public ObservableCollection<InjuredPerson> InjuredPersonnel { get; set; }

Here is a related thread.

public class Investigation : IDisposable, INotifyPropertyChanged
     {
         private List<InjuredPerson> _injuredPersonnel;
         public List<InjuredPerson> InjuredPersonnel  { 
            get {
                return _injuredPersonnel;
            } 
            set
            {
                _injuredPersonnel = value;
                OnPropertyChanged("InjuredPersonnel");
            }
         }
         ...
     }
Community
  • 1
  • 1
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86
  • Hello, when you say "The InjuredPersonnel list needs the OnPropertyChanged event raised on it, not the Investigation (or in addition to)." How do i go about doing that? Also, I have used ObservableCollection - it still hasn't updated – Abrar Ali May 26 '16 at 10:48
  • Heya, That didn't work. Its not triggering the "OnPropertyChanged" event inside Investigation – Abrar Ali May 26 '16 at 11:09
  • Ah, on your SaveInjuredPerson() method you will need to raise the relevant PropertyChanged too. – Murray Foxcroft May 26 '16 at 11:14
  • Woops, I was confused by my own methods! I will try :D – Abrar Ali May 26 '16 at 11:32