方法: プロパティの変更通知を実装するHow to: Implement Property Change Notification

(たとえば、ユーザーがフォームを編集したときに自動的にプレビューペインを更新する)バインディングの動的な変更を自動的にバインディングのターゲットとなるプロパティに反映するために、OneWayまたはTwoWayのバインディングをサポートするには、適切なプロパティ変更通知を提供しなければなりません。To support OneWay or TwoWay binding to enable your binding target properties to automatically reflect the dynamic changes of the binding source (for example, to have the preview pane updated automatically when the user edits a form), your class needs to provide the proper property changed notifications. 次の例は、INotifyPropertyChangedを実装するクラスの作り方を示します。This example shows how to create a class that implements INotifyPropertyChanged.

Example

INotifyPropertyChangedを実装するために、PropertyChangedイベントを宣言して、OnPropertyChangedメソッドを作成します。To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. 次に、変更を通知する必要のある各プロパティについて、そのプロパティが更新されるたびに OnPropertyChanged を呼び出します。Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

using System.ComponentModel;

namespace SDKSample
{
  // This class implements INotifyPropertyChanged
  // to support one-way and two-way bindings
  // (such that the UI element updates when the source
  // has been changed dynamically)
  public class Person : INotifyPropertyChanged
  {
      private string name;
      // Declare the event
      public event PropertyChangedEventHandler PropertyChanged;

      public Person()
      {
      }

      public Person(string value)
      {
          this.name = value;
      }
      
      public string PersonName
      {
          get { return name; }
          set
          {
              name = value;
              // Call OnPropertyChanged whenever the property is updated
              OnPropertyChanged("PersonName");
          }
      }

      // Create the OnPropertyChanged method to raise the event
      protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }
  }
}
Imports System.ComponentModel

' This class implements INotifyPropertyChanged
' to support one-way and two-way bindings
' (such that the UI element updates when the source
' has been changed dynamically)
Public Class Person
    Implements INotifyPropertyChanged

    Private personName As String

    Sub New()
    End Sub

    Sub New(ByVal Name As String)
        Me.personName = Name
    End Sub

    ' Declare the event
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Property Name() As String
        Get
            Return personName
        End Get
        Set(ByVal value As String)
            personName = value
            ' Call OnPropertyChanged whenever the property is updated
            OnPropertyChanged("Name")
        End Set
    End Property

    ' Create the OnPropertyChanged method to raise the event
    Protected Sub OnPropertyChanged(ByVal name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub

End Class

PersonクラスがどのようにTwoWayバインドをサポートしているか、 TextBox テキストで、ソースを更新するタイミングを制御するを参照して下さい。To see an example of how the Person class can be used to support TwoWay binding, see Control When the TextBox Text Updates the Source.

関連項目See also