在我的模型類中,我有幾個列表和其他屬性。其中一個屬性叫做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