データ バインディング [data binding]
DataBinding クラス
アセンブリ: System.Web (system.web.dll 内)
構文
解説
サーバー コントロール内の各データ連結式は、デザイン時に DataBinding クラスのインスタンスによって表されます。1 つ以上のデータ連結式を格納しているサーバー コントロールには、DataBinding オブジェクトを格納している DataBindingCollection オブジェクトがあります。このコレクションには、IDataBindingsAccessor インターフェイスを実装している Control クラスを通じてアクセスできます。カスタム RAD デザイナを作成する場合は、この実装を使用してコレクションにアクセスします。サーバー コントロールに関連付けられている DataBinding オブジェクトまたは DataBindingCollection オブジェクトは、デザイン時にだけ存在します。これらのオブジェクトは実行時には存在しないため、実行時にはアクセスできません。
DataBinding オブジェクトを作成し、propertyName パラメータの値が Text である、コントロールの DataBindingCollection コレクションの既存のオブジェクトと同じようにこのオブジェクトを設定する例を次に示します。propertyName の値が Text の DataBinding オブジェクトがコレクションに含まれている場合、このコードは、オブジェクトの Expression プロパティの値を返します。そのようなオブジェクトがない場合は、空の文字列 ("") が返されます。
' Create the custom class that accesses the DataBinding and ' DataBindingCollection classes at design time. Public Class SimpleDesigner Inherits System.Web.UI.Design.ControlDesigner ' Create a Text property with accessors that obtain ' the property value from and set the property value ' to the Text key in the DataBindingCollection class. Public Property [Text]() As String Get Dim myBinding As DataBinding = DataBindings("Text") If Not (myBinding Is Nothing) Then Return myBinding.Expression End If Return String.Empty End Get Set(ByVal value As String) If value Is Nothing OrElse value.Length = 0 Then DataBindings.Remove("Text") Else Dim binding As DataBinding = DataBindings("Text") If binding Is Nothing Then binding = New DataBinding("Text", GetType(String), value) Else binding.Expression = value End If ' Call the DataBinding constructor, then add ' the initialized DataBinding object to the ' DataBindingCollection for this custom designer. Dim binding1 As DataBinding = CType(DataBindings.SyncRoot, DataBinding) DataBindings.Add(binding) DataBindings.Add(binding1) End If PropertyChanged("Text") End Set End Property Protected Sub PropertyChanged(ByVal propName As String) Dim myHtmlControlDesignBehavior As IControlDesignerTag = Me.Tag Dim myDataBindingCollection As DataBindingCollection Dim myDataBinding1, myDataBinding2 As DataBinding Dim myStringReplace1, myDataBindingExpression1, removedBinding, removedBindingAfterReplace, myDataBindingExpression2, myStringReplace2 As [String] Dim removedBindings1(), removedBindings2() As String Dim temp As Int32 If myHtmlControlDesignBehavior Is Nothing Then Return End If myDataBindingCollection = DataBindings ' Use the DataBindingCollection constructor to ' create the myDataBindingCollection1 object. ' Then set this object equal to the ' DataBindings property of the control created ' by this custom designer. Dim myDataBindingCollection1 As New DataBindingCollection() myDataBindingCollection1 = DataBindings myDataBindingCollection = DataBindings If (myDataBindingCollection.Contains(propName)) Then myDataBinding1 = myDataBindingCollection(propName) myStringReplace1 = propName.Replace(".", "-") If myDataBinding1 Is Nothing Then myHtmlControlDesignBehavior.RemoveAttribute(myStringReplace1) Return End If ' DataBinding is not null. myDataBindingExpression1 = [String].Concat("<%#", myDataBinding1.Expression, "%>") myHtmlControlDesignBehavior.SetAttribute(myStringReplace1, myDataBindingExpression1) Dim index As Integer = myStringReplace1.IndexOf("-") Else ' Use the DataBindingCollection.RemovedBindings ' property to set the value of the removedBindings ' arrays. removedBindings1 = DataBindings.RemovedBindings removedBindings2 = DataBindings.RemovedBindings temp = 0 While removedBindings2.Length > temp removedBinding = removedBindings2(temp) removedBindingAfterReplace = removedBinding.Replace("."c, "-"c) myHtmlControlDesignBehavior.RemoveAttribute(removedBindingAfterReplace) temp = temp & 1 End While End If ' Use the DataBindingCollection.GetEnumerator method ' to iterate through the myDataBindingCollection object ' and write the PropertyName, PropertyType, and Expression ' properties to a file for each DataBinding object ' in the MyDataBindingCollection object. myDataBindingCollection = DataBindings Dim myEnumerator As IEnumerator = myDataBindingCollection.GetEnumerator() While myEnumerator.MoveNext() myDataBinding2 = CType(myEnumerator.Current, DataBinding) Dim dataBindingOutput1, dataBindingOutput2, dataBindingOutput3 As [String] dataBindingOutput1 = [String].Concat("The property name is ", myDataBinding2.PropertyName) dataBindingOutput2 = [String].Concat("The property type is ", myDataBinding2.PropertyType.ToString(), "-", dataBindingOutput1) dataBindingOutput3 = [String].Concat("The expression is ", myDataBinding2.Expression, "-", dataBindingOutput2) WriteToFile(dataBindingOutput3) myDataBindingExpression2 = [String].Concat("<%#", myDataBinding2.Expression, "%>") myStringReplace2 = myDataBinding2.PropertyName.Replace(".", "-") myHtmlControlDesignBehavior.SetAttribute(myStringReplace2, myDataBindingExpression2) Dim index As Integer = myStringReplace2.IndexOf("-"c) End While ' while loop ends End Sub 'OnBindingsCollectionChanged Public Sub WriteToFile(ByVal input As String) ' The WriteToFile custom method writes ' the values of the DataBinding properties ' to a file on the C drive at design time. Dim myFile As StreamWriter = File.AppendText("C:\DataBindingOutput.txt") Dim encoder As New ASCIIEncoding() Dim ByteArray As Byte() = encoder.GetBytes(input) Dim CharArray As Char() = encoder.GetChars(ByteArray) myFile.WriteLine(CharArray, 0, input.Length) myFile.Close() End Sub 'WriteToFile End Class 'SimpleDesigner
// Create the custom class that accesses the DataBinding and // DataBindingCollection classes at design time. public class SimpleDesigner : System.Web.UI.Design.ControlDesigner { // Create a Text property with accessors that obtain // the property value from and set the property value // to the Text key in the DataBindingCollection class. public string Text { get { DataBinding myBinding = DataBindings["Text"]; if (myBinding != null) { return myBinding.Expression; } return String.Empty; } set { if ((value == null) || (value.Length == 0)) { DataBindings.Remove("Text"); } else { DataBinding binding = DataBindings["Text"]; if (binding == null) { binding = new DataBinding("Text", typeof(string), value); } else { binding.Expression = value; } // Call the DataBinding constructor, then add // the initialized DataBinding object to the // DataBindingCollection for this custom designer. DataBinding binding1 = (DataBinding)DataBindings.SyncRoot; DataBindings.Add(binding); DataBindings.Add(binding1); } PropertyChanged("Text"); } } protected void PropertyChanged(string propName) { IControlDesignerTag myHtmlControlDesignBehavior = this.Tag; DataBindingCollection myDataBindingCollection; DataBinding myDataBinding1, myDataBinding2; String myStringReplace1, myDataBindingExpression1, removedBinding, removedBindingAfterReplace, myDataBindingExpression2, myStringReplace2; string[] removedBindings1, removedBindings2; Int32 temp; if (myHtmlControlDesignBehavior == null) return; // Use the DataBindingCollection constructor to // create the myDataBindingCollection1 object. // Then set this object equal to the // DataBindings property of the control created // by this custom designer. DataBindingCollection myDataBindingCollection1 = new DataBindingCollection(); myDataBindingCollection1 = myDataBindingCollection = DataBindings; if (myDataBindingCollection.Contains(propName)) { myDataBinding1 = myDataBindingCollection[propName]; myStringReplace1 = propName.Replace(".", "-"); if (myDataBinding1 == null) { myHtmlControlDesignBehavior.RemoveAttribute(myStringReplace1); return; } // DataBinding is not null. myDataBindingExpression1 = String.Concat("<%#", myDataBinding1.Expression, "%>"); myHtmlControlDesignBehavior.SetAttribute(myStringReplace1, myDataBindingExpression1); int index = myStringReplace1.IndexOf("-"); } else { // Use the DataBindingCollection.RemovedBindings // property to set the value of the removedBindings // arrays. removedBindings2 = removedBindings1 = DataBindings.RemovedBindings; temp = 0; while (removedBindings2.Length > temp) { removedBinding = removedBindings2[temp]; removedBindingAfterReplace = removedBinding.Replace('.', '-'); myHtmlControlDesignBehavior.RemoveAttribute(removedBindingAfterReplace); temp = temp + 1; } } // Use the DataBindingCollection.GetEnumerator method // to iterate through the myDataBindingCollection object // and write the PropertyName, PropertyType, and Expression // properties to a file for each DataBinding object // in the MyDataBindingCollection object. myDataBindingCollection = DataBindings; IEnumerator myEnumerator = myDataBindingCollection.GetEnumerator(); while (myEnumerator.MoveNext()) { myDataBinding2 = (DataBinding)myEnumerator.Current; String dataBindingOutput1, dataBindingOutput2, dataBindingOutput3; dataBindingOutput1 = String.Concat("The property name is ", myDataBinding2.PropertyName); dataBindingOutput2 = String.Concat("The property type is ", myDataBinding2.PropertyType.ToString(), "-", dataBindingOutput1); dataBindingOutput3 = String.Concat("The expression is ", myDataBinding2.Expression, "-", dataBindingOutput2); WriteToFile(dataBindingOutput3); myDataBindingExpression2 = String.Concat("<%#", myDataBinding2.Expression, "%>"); myStringReplace2 = myDataBinding2.PropertyName.Replace(".", "-"); myHtmlControlDesignBehavior.SetAttribute(myStringReplace2, myDataBindingExpression2); int index = myStringReplace2.IndexOf('-'); }// while loop ends } public void WriteToFile(string input) { // The WriteToFile custom method writes // the values of the DataBinding properties // to a file on the C drive at design time. StreamWriter myFile = File.AppendText("C:\\DataBindingOutput.txt"); ASCIIEncoding encoder = new ASCIIEncoding(); byte[] ByteArray = encoder.GetBytes(input); char[] CharArray = encoder.GetChars(ByteArray); myFile.WriteLine(CharArray, 0, input.Length); myFile.Close(); } }
// Create the custom class that accesses the DataBinding and // DataBindingCollection classes at design time. public class SimpleDesigner extends System.Web.UI.Design.ControlDesigner { // Create a Text property with accessors that obtain // the property value from and set the property value // to the Text key in the DataBindingCollection class. /** @property */ public String get_Text() { DataBinding myBinding = get_DataBindings().get_Item("Text"); if (myBinding != null) { return myBinding.get_Expression(); } return(""); } //get_Text /** @property */ public void set_Text(String value) { if (value == null || value.get_Length() == 0) { get_DataBindings().Remove("Text"); } else { DataBinding binding = get_DataBindings().get_Item("Text"); if (binding == null) { binding = new DataBinding("Text", String.class.ToType(), value); } else { binding.set_Expression(value); } // Call the DataBinding constructor, then add // the initialized DataBinding object to the // DataBindingCollection for this custom designer. DataBinding binding1 = (DataBinding)(get_DataBindings(). get_SyncRoot()); get_DataBindings().Add(binding); get_DataBindings().Add(binding1); } OnBindingsCollectionChanged("Text"); } //set_Text // Override the OnBindingsCollectionChanged class to create // the data-binding expression and associate it with // a property on the control created by the designer. protected void OnBindingsCollectionChanged(String propName) { IHtmlControlDesignerBehavior myHtmlControlDesignBehavior = get_Behavior(); DataBindingCollection myDataBindingCollection; DataBinding myDataBinding1, myDataBinding2; String myStringReplace1, myDataBindingExpression1, removedBinding, removedBindingAfterReplace, myDataBindingExpression2, myStringReplace2; String removedBindings1[], removedBindings2[]; int temp; if (myHtmlControlDesignBehavior == null) { return; } // Use the DataBindingCollection constructor to // create the myDataBindingCollection1 object. // Then set this object equal to the // DataBindings property of the control created // by this custom designer. DataBindingCollection myDataBindingCollection1 = new DataBindingCollection(); myDataBindingCollection1 = (myDataBindingCollection = get_DataBindings()); if (propName != null) { myDataBinding1 = myDataBindingCollection.get_Item(propName); myStringReplace1 = propName.Replace(".", "-"); if (myDataBinding1 == null) { myHtmlControlDesignBehavior.RemoveAttribute(myStringReplace1, true); return; } // DataBinding is not null. myDataBindingExpression1 = String.Concat("<%#", myDataBinding1. get_Expression(), "%>"); myHtmlControlDesignBehavior.SetAttribute(myStringReplace1, myDataBindingExpression1, true); int index = myStringReplace1.IndexOf("-"); } else { // Use the DataBindingCollection.RemovedBindings // property to set the value of the removedBindings // arrays. removedBindings2 = (removedBindings1 = get_DataBindings(). get_RemovedBindings()); temp = 0; while (removedBindings2.length > temp) { removedBinding = removedBindings2[temp]; removedBindingAfterReplace = removedBinding.Replace('.', '-'); myHtmlControlDesignBehavior.RemoveAttribute( removedBindingAfterReplace, true); temp = temp + 1; } } // Use the DataBindingCollection.GetEnumerator method // to iterate through the myDataBindingCollection object // and write the PropertyName, PropertyType, and Expression // properties to a file for each DataBinding object // in the MyDataBindingCollection object. myDataBindingCollection = get_DataBindings(); IEnumerator myEnumerator = myDataBindingCollection.GetEnumerator(); while (myEnumerator.MoveNext()) { myDataBinding2 = (DataBinding)(myEnumerator.get_Current()); String dataBindingOutput1, dataBindingOutput2, dataBindingOutput3; dataBindingOutput1 = String.Concat("The property name is ", myDataBinding2.get_PropertyName()); dataBindingOutput2 = String.Concat("The property type is ", myDataBinding2.get_PropertyType().ToString(), "-", dataBindingOutput1); dataBindingOutput3 = String.Concat("The expression is ", myDataBinding2.get_Expression(), "-", dataBindingOutput2); WriteToFile(dataBindingOutput3); myDataBindingExpression2 = String.Concat("<%#", myDataBinding2. get_Expression(), "%>"); myStringReplace2 = myDataBinding2.get_PropertyName(). Replace(".", "-"); myHtmlControlDesignBehavior.SetAttribute(myStringReplace2, myDataBindingExpression2, true); int index = myStringReplace2.IndexOf("-"); } // while loop ends } //OnBindingsCollectionChanged public void WriteToFile(String input) { // The WriteToFile custom method writes // the values of the DataBinding properties // to a file on the C drive at design time. StreamWriter myFile = File.AppendText("C:\\DataBindingOutput.txt"); ASCIIEncoding encoder = new ASCIIEncoding(); ubyte byteArray[] = encoder.GetBytes(input); char charArray[] = encoder.GetChars(byteArray); myFile.WriteLine(charArray, 0, input.get_Length()); myFile.Close(); } //WriteToFile
System.Web.UI.DataBinding
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
DataBinding コンストラクタ
アセンブリ: System.Web (system.web.dll 内)
構文
Dim propertyName As String Dim propertyType As Type Dim expression As String Dim instance As New DataBinding(propertyName, propertyType, expression)
- propertyType
DataBinding オブジェクトを作成し、propertyName パラメータの値が Text である、コントロールの DataBindingCollection コレクションの既存のオブジェクトと同じようにこのオブジェクトを設定する例を次に示します。propertyName の値が Text の DataBinding オブジェクトがコレクションに含まれている場合、このコードは、オブジェクトの Expression プロパティの値を返します。そのようなオブジェクトがない場合は、空の文字列 ("") が返されます。
' Create a Text property with accessors that obtain ' the property value from and set the property value ' to the Text key in the DataBindingCollection class. Public Property [Text]() As String Get Dim myBinding As DataBinding = DataBindings("Text") If Not (myBinding Is Nothing) Then Return myBinding.Expression End If Return String.Empty End Get Set(ByVal value As String) If value Is Nothing OrElse value.Length = 0 Then DataBindings.Remove("Text") Else Dim binding As DataBinding = DataBindings("Text") If binding Is Nothing Then binding = New DataBinding("Text", GetType(String), value) Else binding.Expression = value End If ' Call the DataBinding constructor, then add ' the initialized DataBinding object to the ' DataBindingCollection for this custom designer. Dim binding1 As DataBinding = CType(DataBindings.SyncRoot, DataBinding) DataBindings.Add(binding) DataBindings.Add(binding1) End If PropertyChanged("Text") End Set End Property
// Create a Text property with accessors that obtain // the property value from and set the property value // to the Text key in the DataBindingCollection class. public string Text { get { DataBinding myBinding = DataBindings["Text"]; if (myBinding != null) { return myBinding.Expression; } return String.Empty; } set { if ((value == null) || (value.Length == 0)) { DataBindings.Remove("Text"); } else { DataBinding binding = DataBindings["Text"]; if (binding == null) { binding = new DataBinding("Text", typeof(string), value); } else { binding.Expression = value; } // Call the DataBinding constructor, then add // the initialized DataBinding object to the // DataBindingCollection for this custom designer. DataBinding binding1 = (DataBinding)DataBindings.SyncRoot; DataBindings.Add(binding); DataBindings.Add(binding1); } PropertyChanged("Text"); } }
// Create a Text property with accessors that obtain // the property value from and set the property value // to the Text key in the DataBindingCollection class. /** @property */ public String get_Text() { DataBinding myBinding = get_DataBindings().get_Item("Text"); if (myBinding != null) { return myBinding.get_Expression(); } return(""); } //get_Text /** @property */ public void set_Text(String value) { if (value == null || value.get_Length() == 0) { get_DataBindings().Remove("Text"); } else { DataBinding binding = get_DataBindings().get_Item("Text"); if (binding == null) { binding = new DataBinding("Text", String.class.ToType(), value); } else { binding.set_Expression(value); } // Call the DataBinding constructor, then add // the initialized DataBinding object to the // DataBindingCollection for this custom designer. DataBinding binding1 = (DataBinding)(get_DataBindings(). get_SyncRoot()); get_DataBindings().Add(binding); get_DataBindings().Add(binding1); } OnBindingsCollectionChanged("Text"); } //set_Text
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
DataBinding プロパティ
パブリック プロパティ
名前 | 説明 | |
---|---|---|
Expression | 評価されるデータ連結式を取得します。 | |
PropertyName | データを連結する先の ASP.NET サーバー コントロール プロパティの名前を取得します。 | |
PropertyType | データ連結 ASP.NET サーバー コントロール プロパティの .NET Framework 型を取得します。 |
DataBinding メソッド
パブリック メソッド
名前 | 説明 | |
---|---|---|
Equals | オーバーロードされます。 オーバーライドされます。 | |
GetHashCode | オーバーライドされます。 DataBinding オブジェクトのインスタンスのハッシュ コードを取得します。 | |
GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) | |
ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |
名前 | 説明 | |
---|---|---|
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
DataBinding メンバ
Microsoft Visual Studio などの RAD (Rapid Application Development) デザイナがデザイン時にデータ バインディング式を作成できるようにする ASP.NET サーバー コントロールの単一のデータ バインディング式に関する情報を格納します。このクラスは継承できません。
DataBinding データ型で公開されるメンバを以下の表に示します。
パブリック コンストラクタ
パブリック プロパティ
名前 | 説明 | |
---|---|---|
Expression | 評価されるデータ連結式を取得します。 | |
PropertyName | データを連結する先の ASP.NET サーバー コントロール プロパティの名前を取得します。 | |
PropertyType | データ連結 ASP.NET サーバー コントロール プロパティの .NET Framework 型を取得します。 |
名前 | 説明 | |
---|---|---|
Equals | オーバーロードされます。 オーバーライドされます。 | |
GetHashCode | オーバーライドされます。 DataBinding オブジェクトのインスタンスのハッシュ コードを取得します。 | |
GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) | |
ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) | |
ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |
名前 | 説明 | |
---|---|---|
Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) | |
MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
データバインディング
(data binding から転送)
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2023/01/12 09:42 UTC 版)
データバインディング(データバインド、データ結合、英: data binding)は、コンピュータプログラミングにおいて、データ(ソースオブジェクト)とそれに対応する対象要素(ターゲットオブジェクト)を結びつけ、データあるいは対象の変更を暗黙的に(自動的に)もう一方に反映(同期)することであり、またそれを実現する仕組みのことである[1]。データバインディングは特にGUIを持つアプリケーションソフトウェアの効率的な開発を目的とした技術であり、Model-View-ViewModel (MVVM) パターンの実現に必須の技術でもある[2]。
- ^ “データ バインディングの概要 - WPF .NET Framework”. Microsoft Docs (2020年12月3日). 2021年9月29日閲覧。 “データ バインディングとは、アプリの UI と、そこに表示されるデータとの間の接続を確立する処理です。 バインドが適切に設定され、データから適切な通知が提供される場合、データの値が変更されると、そのデータにバインドされている要素に変更が自動的に反映されます。”
- ^ “Xamarin.Forms のデータ バインディング - Xamarin”. Microsoft Docs (2020年5月21日). 2021年9月29日閲覧。 “データ バインディングは、2 つのオブジェクトのプロパティをリンクして、片方のプロパティへの変更が自動的にもう片方のプロパティに反映されるようにする手法です。データ バインディングは、Model-View-ViewModel (MVVM) アプリケーション アーキテクチャにとって不可欠の部分です。”
- ^ データバインディングは、Custom Element(ホストエレメント)のデータとそのローカルDOM(子エレメントまたはターゲットエレメント)のプロパティまたは属性にコネクトします。 データバインディング. Polymer Japan
- ^ リアクティブシステムです。モデルは単なるプレーンな JavaScript オブジェクトです。それらを変更するとビューが更新されます。 リアクティブの探求 - Vue.js
- ^ reactive UIs using the React framework View integrations - Apollo
- ^ 方法: バインディングの方向を指定する - WPF .NET Framework | Microsoft Docs
- ^ Xamarin.Forms のバインディング モード - Xamarin | Microsoft Docs
- ^ 通常、関数が終了すると変数は『消えて』しまいますが、state 変数は React によって保持されます。ステートフックの利用法 - React
- ^ それぞれのコンポーネントに関連付けられる形で、React 内に「メモリーセル」のリストが存在しています。...
useState()
のようなフックを呼ぶと、フックは現在のセルの値を読み出し(あるいは初回レンダー時はセル内容を初期化し)、ポインタを次に進めます フックに関するよくある質問 - React - ^ データ バインディング ライブラリ | Android デベロッパー | Android Developers
- ^ データ バインディング - ADO.NET | Microsoft Docs
- ^ データ バインディング - Windows Forms .NET Framework | Microsoft Docs
- 1 データバインディングとは
- 2 データバインディングの概要
- 3 データバインディングの実装例
- 4 脚注
- data bindingのページへのリンク