2016-12-08 155 views
0

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

嗨,你會介意如何在XAML的數據綁定,爲那些非更新屬性? - 謝謝 – quicoli

+0

當然,將它添加到帖子的末尾。 – pfinferno

+1

一個選項是簡單地替換'OnPropertyChanged(「CurrentVehLat」); OnPropertyChanged(「CurrentVehLong」);'在CurrentIteration'設置器中調用其他設置器(它將調用它們自己的OnPropertyChanged):'CurrentVehLat = double.MaxValue;','CurrentVehLong = double.MinValue;'沒關係,你從來沒有讀過那些制定者的「價值」)。通過這種方式,任何時候CurrentIteration都被賦值,他的setter調用其他屬性的setter來獲取更新後的'CurrentIteration'值。 – Quantic

回答

2

「這些特性發生了改變,重新查詢他們的價值觀」。這意味着它調用get訪問器方法。

它看起來像你期待它調用set方法,這是不會發生,因爲你沒有設置值。

嘗試取出後盾財產,只是直接訪問數組值,像這樣:

public double CurrentVehLong 
{ 
    get { return VehLong[CurrentIteration];; } 
    set 
    { 
     VehLong[CurrentIteration] = value; 
     OnPropertyChanged("CurrentVehLong"); 
    } 
} 

現在,當你調用OnPropertyChanged("CurrentVehLong"),它將重新查詢get訪問此屬性,並更新基於價值在CurrentIteration。我也修改了set方法,所以它會更有意義,如果這是您想要在其他位置執行的操作,則可以使用它來設置值。

+0

工程太棒了!我認爲我對這些問題的理解有點淺薄,我覺得我總是需要一個支持價值。那麼他們的重點究竟是什麼呢? (如果這是一個很長的答案,不用擔心)。 – pfinferno

+1

@pfinferno它只是一種存儲內部訪問數據的方式,所以您不必經過常規的get/set存取方法。這樣做的最常見原因是爲了避免任何形式的無限循環,例如[此答案顯示](http://stackoverflow.com/a/3272900/302677)顯示。你也可以使用自動屬性,例如'{get;組; }'這將導致編譯器爲你的內部生成後臺字段,儘管在WPF的情況下你通常需要在setter中設置PropertyChange通知,所以你自己寫出來。 – Rachel