NotificationObject クラス
プロパティ変更を View 側に伝達するために、ViewModel または Model は INotifyPropertyChanged インターフェースを実装しなければなりません。しかし、このインターフェースを実装するために毎回同じコードを書くべきではないので、あらかじめこのインターフェースを実装したクラスとして NotificationObject クラスを次のように定義します。
using System.ComponentModel;
using System.Runtime.CompilerServices;
/// <summary>
/// INotifyPropertyChanged インターフェースを実装した抽象クラスを表します。
/// </summary>
public abstract class NotificationObject : INotifyPropertyChanged
{
#region INotifyPropertyChanged のメンバ
/// <summary>
/// プロパティ変更時に発生します。
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
#endregion INotifyPropertyChanged のメンバ
/// <summary>
/// PropertyChanged イベントを発行します。
/// </summary>
/// <param name="propertyName">プロパティ名を指定します。</param>
protected void RaisePropertyChanged([CallerMemberName]string propertyName = null)
{
var h = this.PropertyChanged;
if (h != null) h(this, new PropertyChangedEventArgs(propertyName));
}
/// <summary>
/// プロパティ値変更ヘルパです。
/// </summary>
/// <typeparam name="T">プロパティの型を表します。</typeparam>
/// <param name="target">変更するプロパティの実体を指定します。</param>
/// <param name="value">変更後の値を指定します。</param>
/// <param name="propertyName">プロパティ名を指定します。</param>
/// <returns>プロパティ値に変更があった場合に true を返します。</returns>
protected bool SetProperty<T>(ref T target, T value, [CallerMemberName]string propertyName = null)
{
if (Equals(target, value)) { return false; }
target = value;
RaisePropertyChanged(propertyName);
return true;
}
}
INotifyPropertyChanged インターフェースのメンバは PropertyChanged イベントだけですが、このイベントを発生させるためのメソッドを 2 つ用意しています。
RaisePropertyChanged() メソッドは、変更があったプロパティ名を指定してコールすることで、そのプロパティが変更したことを View に伝えます。プロパティ名を省略した場合は、すべてのプロパティが変更されたとして伝えられます。ただし、CallberMemberName 属性をサポートするコンパイラによってコンパイルした場合、プロパティ名を省略してもそのプロパティが変更されたとして通知されます。
SetProperty() メソッドは、プロパティ値を変更するためのヘルパです。上記の RaisePropertyChanged() メソッドは、プロパティ値をセットするときは必ずコールされるメソッドになります。したがって、プロパティ値をセットするコードと一緒にしてしまえば、プロパティをセットするコードと RaisePropertyChanged() メソッドをコールするコードが一つにまとまります。
これらのメソッドの使用例は次のようになります。
/// <summary>
/// プロパティ変更の使用例を示すためのクラスを表します。
/// </summary>
public class SampleViewModel : NotificationObject
{
private int _id;
/// <summary>
/// 識別子を取得または設定します。
/// </summary>
public int ID
{
get { return this._id; }
set
{
// プロパティ値に変更があるかどうかを確認します。
if (this._id != value)
{
this._id = value;
// 変更があった場合に変更通知をおこないます。
RaisePropertyChanged("ID");
}
}
}
private int _age;
/// <summary>
/// 年齢を取得または設定します。
/// </summary>
public int Age
{
get { return this._age; }
set { SetProperty(ref this._age, value); }
}
private string _firstName;
/// <summary>
/// 名を取得または設定します。
/// </summary>
public string FirstName
{
get { return this._firstName; }
set
{
// プロパティ値に変更があった場合に true が返ります。
if (SetProperty(ref this._firstName, value))
{
// 同時に FullName プロパティの変更通知をおこないます。
RaisePropertyChanged("FullName");
}
}
}
private string _lastName;
/// <summary>
/// 姓を取得または設定します。
/// </summary>
public string LastName
{
get { return this._lastName; }
set
{
// プロパティ値に変更があった場合に true が返ります。
if (SetProperty(ref this._lastName, value))
{
// 同時に FullName プロパティの変更通知をおこないます。
RaisePropertyChanged("FullName");
}
}
}
/// <summary>
/// 氏名を取得します。
/// </summary>
public string FullName
{
get { return string.Format("{0} {1}", this.FirstName, this.LastName); }
}
}
SetProperty() メソッドを使わずにプロパティ値の変更をおこなう場合、ID プロパティの set アクセサのように、現在の値と比較して、異なる場合に新しい値を代入し、その変更通知を RaisePropertyChanged() メソッドでおこなわなければいけません。
これに対し、SetProperty() メソッドは、Age プロパティの set アクセサのように、プロパティ値の変更とその変更通知をたったの一行で書くことができるようになります。通常のプロパティはこの Age プロパティのような書き方をお勧めします。
実際には、FullName プロパティのように、他のプロパティ値に依存して変更されるプロパティもあります。この場合、他のプロパティ値が変更されたときに、FullName プロパティの変更通知をおこなわなければなりません。このような場合でも、SetProperty() メソッドが有用となります。FirstName プロパティや LastName プロパティの set アクセサのように、SetProperty() メソッドの戻り値によって、自分のプロパティ値が変更された場合は、その変更が反映されるように RaisePropertyChanged() メソッドの引数に "FullName" を与え、自分の変更通知の後に FullName プロパティの変更通知もおこなうようにできます。
Tweet