我正在努力使用DataBindings。我有一个ListView(正确显示)有项目,我需要能够编辑列表中的项目。
我选择了一个项目,它打开一个模态(工作正常)与所选项目的信息(工作正常)。当我单击“保存”时,项目不会更新 - 显示不会更新,但如果我再次选择该项目,则会正确保存数据。
我有以下对象:
public class Investigation : IDisposable
{
public List<InjuredPerson> InjuredPersonnel { get; set; }
...
}
我的ViewModel是这样的:
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();
}
}
XAML:
<ListView ItemsSource="{Binding Investigation.InjuredPersonnel, Mode=TwoWay}">
...
更新信息的页面发送如下消息:(正常工作)
MessagingCenter.Send<EditInjuredPerson, InjuredPerson>(this, "InjuredPersonEdited", _injuredPerson);
接收方(工作正常)
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;
}
}
答案 0 :(得分:0)
InjuredPersonnel列表需要在其上引发OnProprertyChanged事件,而不是调查(或者除此之外)。
或者,转换List&lt;&gt;到ObservableCollection&lt;&gt ;.
public List<InjuredPerson> InjuredPersonnel { get; set; }
变为
public ObservableCollection<InjuredPerson> InjuredPersonnel { get; set; }
public class Investigation : IDisposable, INotifyPropertyChanged
{
private List<InjuredPerson> _injuredPersonnel;
public List<InjuredPerson> InjuredPersonnel {
get {
return _injuredPersonnel;
}
set
{
_injuredPersonnel = value;
OnPropertyChanged("InjuredPersonnel");
}
}
...
}