Reputation: 1945
In my model class, I have several lists and other properties. One of these properties is called CurrentIteration
and constantly gets updated. When this gets updated, I want other properties to update themselves to the element of a corresponding list that is the index of CurrentIteration
. I thought all's I needed to include was an OnPropertyChanged
event for the properties I want to update in the setter of CurrentIteration
. However, they don't seem to be getting called.
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 get updated properly, but the rest do not. The setters get skipped over completely. I'm almost positive it's something simple and my understanding of the setters in this case is wrong, but I'm not sure what it is exactly.
Edit: Here's an example of one of the bindings in XAML:
Text="{Binding Path=CurrentVehLong,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
Upvotes: 2
Views: 6324
Reputation: 132558
Raising the property change notification just says "These properties have changed, re-query their values". Meaning it calls the get
accessor method.
It looks like you are expecting it to call the set
method, which is not going to happen because you are not setting the value.
Try removing the backing property and just access the array value directly, like this :
public double CurrentVehLong
{
get { return VehLong[CurrentIteration];; }
set
{
VehLong[CurrentIteration] = value;
OnPropertyChanged("CurrentVehLong");
}
}
Now when you call OnPropertyChanged("CurrentVehLong")
, it will re-query the get
accessor for this property, and update the value based on the CurrentIteration
. I also altered the set
method so it would make more sense, and you'd be able to use it to set the value if that's what you want to do elsewhere.
Upvotes: 2