私は WPF とバインディングが初めてで、配列要素とコントロールの間で双方向バインディングを行う方法があるかどうかを確認したかったのです。
私の ViewModel には、次のようなプロパティがあります。
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");
}
}
MeasurementValues は次のように定義されます。
public class MeaseurementValue
{
public string TestName { get; set; }
public int value { get;set; }
}
Xaml は次のようになります。
<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" />
したがって、xaml の要素は ViewModel の Arrayelement にバインドされます。しかし、テキスト コントロールでそのプロパティを変更すると、ViewModel のセッターが呼び出されません。
また、配列内のすべての要素も通知可能になるようにコードを変更したため、次のようになります。
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));
}
}
}
しかし、それもうまくいきませんでした
プリミティブ型とは対照的に、配列で行う必要があることは何か違いがありますか?