I am new to WPF and bindings and I wanted to see if there is a way to do a two way binding between an array element and a control:
My ViewModel has a property which looks like following:
public ObservableCollection<MeaseurementValue> MeasurementValues
{
get
{
return Config.MeasurementValues;
}
set
{
if (value == null) return;
Config.MeasurementValues = value;
OnPropertyChanged("MeasurementValues");
QpatConfig.SerializeConfigFile(Config,
Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System)) + "//Qualcomm//QPAT//qpat.config");
}
}
Where MeasurementValues is defined as following:
public class MeaseurementValue
{
public string TestName { get; set; }
public int value { get;set; }
}
Xaml looks like following:
<TextBox HorizontalAlignment="Left" Grid.Column="1" Grid.Row="1" Height="23" Margin="14,20,0,0" TextWrapping="Wrap" Text="{Binding MeasurementValues[0].value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="124" />
So the element in the xaml is bound to an Arrayelement in ViewModel. But when I change that property in text control it does not call the setter of the ViewModel.
I also changed my code that evey element in the array is also notifiable so it looks like following:
public class MeaseurementValue : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string TestName { get; set; }
private int _value;
public int value
{
get { return _value; }
set { _value = value; OnPropertyChanged("value"); }
}
void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
But that also did not work
Is that anything different that we have to do with Arrays as oppose to primitive types?
MeasurementValuesbecause it's not replacing the collection object.valuewould get called, because that's what is being updated.