以下は、プロパティとして使用できるジェネリック クラスであり、プロパティ値の変更をキャプチャするためにバインドされたコントロールで使用される INotifyPropertyChanged を実装します。
public class NotifyValue<datatype> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
datatype _value;
public datatype Value
{
get
{
return _value;
}
set
{
_value = value;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Value"));
}
}
}
次のように宣言できます。
public NotifyValue<int> myInteger = new NotifyValue<int>();
このようなテキストボックスに割り当てられます
Textbox1.DataBindings.Add(
"Text",
this,
"myInteger.Value",
false,
DataSourceUpdateMode.OnPropertyChanged
);
..ここで、「テキスト」はテキストボックスのプロパティ、「これ」は現在のフォーム インスタンスです。
クラスは INotifyPropertyChanged クラスを継承する必要はありません。タイプ System.ComponentModel.PropertyChangedEventHandler のイベントを宣言すると、クラス変更イベントがコントロール データバインダーによってサブスクライブされます。