2016-12-08 153 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