在我的模型类中,我有几个列表和其他属性。其中一个属性叫做CurrentIteration
,并且不断更新。当这个更新时,我希望其他属性将自己更新到索引为CurrentIteration
的相应列表的元素。我认为我需要包括的一个OnPropertyChanged
事件是我想要在CurrentIteration
的设置器中更新的属性。但是,他们似乎没有被召唤。另一个属性更改时更新几个属性?
public class VehicleModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private List<double> _nowTime = new List<double>();
public List<double> NowTime
{
get { return this._nowTime; }
set { this._nowTime = value; OnPropertyChanged("Nowtime"); }
}
private List<double> _VehLat = new List<double>();
public List<double> VehLat
{
get { return this._VehLat; }
set { this._VehLat = value; OnPropertyChanged("VehLat"); }
}
private List<double> _VehLong = new List<double>();
public List<double> VehLong
{
get { return _VehLong; }
set { _VehLong = value; OnPropertyChanged("VehLong"); }
}
//non-list properties
private int _currentIteration;
public int CurrentIteration //used to hold current index of the list of data fields
{
get { return _currentIteration; }
set
{
_currentIteration = value;
OnPropertyChanged("CurrentIteration");
OnPropertyChanged("CurrentVehLat");
OnPropertyChanged("CurrentVehLong");
}
}
private double _currentVehLat;
public double CurrentVehLat
{
get { return _currentVehLat; }
set { _currentVehLat = VehLat[CurrentIteration]; OnPropertyChanged("CurrentVehLat"); }
}
private double _currentVehLong;
public double CurrentVehLong
{
get { return _currentVehLong; }
set { _currentVehLong = VehLong[CurrentIteration]; OnPropertyChanged("CurrentVehLong"); }
}
public void SetData(int i)
{
CurrentIteration = i;
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
CurrentIteration
DOES得到正确更新,但其余的都没有。设置者完全跳过。我几乎是积极的,这是简单的,我在这种情况下对制定者的理解是错误的,但我不确定它到底是什么。
编辑:下面是XAML绑定的一个示例:提高属性更改通知只是说
Text="{Binding Path=CurrentVehLong,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
嗨,你会介意如何在XAML的数据绑定,为那些非更新属性? - 谢谢 – quicoli
当然,将它添加到帖子的末尾。 – pfinferno
一个选项是简单地替换'OnPropertyChanged(“CurrentVehLat”); OnPropertyChanged(“CurrentVehLong”);'在CurrentIteration'设置器中调用其他设置器(它将调用它们自己的OnPropertyChanged):'CurrentVehLat = double.MaxValue;','CurrentVehLong = double.MinValue;'没关系,你从来没有读过那些制定者的“价值”)。通过这种方式,任何时候CurrentIteration都被赋值,他的setter调用其他属性的setter来获取更新后的'CurrentIteration'值。 – Quantic