Skip to content

Instantly share code, notes, and snippets.

@peace2048
Last active March 2, 2016 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peace2048/9b4f21fbbdcb46760a4f to your computer and use it in GitHub Desktop.
Save peace2048/9b4f21fbbdcb46760a4f to your computer and use it in GitHub Desktop.
forms_databinding.md
Imports System.ComponentModel
Public Class Form1
Public Class Model
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(NameOf(Name)))
End Set
End Property
End Class
End Class

Windows Forms のデータバインディング

WPF では当然のように使用されるデータバインディングですが、Forms でもデータをバインドできます。 そこで、簡単なプログラムを作成してみます。

作成するプログラムは、名前を入力しボタンをクリックしたら、入力された名前をメッセージボックスに出力するだけのプログラムです。

モデルの作成

バインドするモデルを作成します。 今回は、Name プロパティを持つクラスを Form クラスの内部に作成してみます。入れ子にする意味はありません。 Name プロパティの値が変わったときに画面の表示も更新したいので、INotifyPropertyChanged も実装します。

バインドの設定

フォームデザイナでバインドを設定する場合は、モデルクラス

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment