Share via


Web フォーム データ連結コントロール デザイナの実装

データ連結コントロール デザイナの主なタスクは、コントロールがデザイナ内に存在するとき、そのコントロールが別の (サンプル) デザイン時データ ソースにアクセスできるようにすることです。.NET Framework には、データ ソースへのアクセスを有効にするコントラクトを指定するインターフェイス (IDataSourceProvider) が用意されています。データ連結サーバー コントロールのデザイナは、IDataSourceProvider インターフェイスおよび以下の手順で説明するその他の主要な機能を実装する必要があります。

データ連結コントロール デザイナを実装するには、次のようにします。

メモ   以下の手順では、「Web フォーム テンプレート データ連結コントロール デザイナのサンプル」に示す特定の実装について説明します。この実装は、IEnumerable 参照を受け入れる object 型のデータ ソースを持つコントロールに適用できます。

  1. ControlDesigner から直接または間接的に派生するクラスを定義し、次のコードで示すように IDataSourceProvider インターフェイスを実装します。

    Public Class DataboundControlDesigner
       Inherits ControlDesigner
       Implements IDataSourceProvider
       ...
    End Class
    [C#]
    public class DataboundControlDesigner : ControlDesigner, IDataSourceProvider {...}
    
  2. デザイン時データ ソースを指定する string 型のプロパティ DataSource を公開します。このプロパティの実装方法の例については、「Web フォーム テンプレート データ連結コントロール デザイナのサンプル」を参照してください。デザイナの DataSource プロパティは、コントロールの DataBindings コレクションと共に機能します。

  3. GetDesignTimeHtml メソッドをオーバーライドし、デザイナに関連付けられているコントロールのデータ ソースをデザイン時データ ソースに設定します。このメソッドをオーバーライドしてデータ ソースを置き換える方法の例については、「Web フォーム テンプレート データ連結コントロール デザイナのサンプル」を参照してください。

  4. PreFilterProperties メソッドをオーバーライドして、コントロールの DataSource プロパティをデザイン時の DataSource プロパティで置換またはシャドウします。PreFilterProperties メソッドは IDesignerFilter インターフェイスに属します。このインターフェイスは、デザイナがデザイン時にプロパティおよびイベントを置換または作成できるようにします。IDesignerFilterControlDesigner によって実装されます。プロパティのフィルタ処理の詳細については、「メタデータ フィルタ処理」を参照してください。PreFilterProperties メソッドをオーバーライドする方法を示すコードを次に示します。

    Protected Overrides Sub PreFilterProperties(properties As IDictionary)
       MyBase.PreFilterProperties(properties)
    
       Dim prop As PropertyDescriptor
    
       prop = CType(properties("DataSource"), PropertyDescriptor)
       Debug.Assert(( Not (prop Is Nothing)))
       prop = TypeDescriptor.CreateProperty(Me.GetType(), prop, _
             New Attribute() {New TypeConverterAttribute(GetType(DataSourceConverter))})
    
       properties("DataSource") = prop
    End Sub
    [C#]
    protected override void PreFilterProperties(IDictionary properties) {
                base.PreFilterProperties(properties);
    
                PropertyDescriptor prop;
    
                prop = (PropertyDescriptor)properties["DataSource"];
                Debug.Assert(prop != null);
                prop = TypeDescriptor.CreateProperty(this.GetType(), prop,
                                                     new Attribute[] {
                                                         new TypeConverterAttribute(typeof(DataSourceConverter))
                                                     });
                properties["DataSource"] = prop;
            }
    
  5. IDataSourceProvider インターフェイスのメソッドを実装します。このインターフェイスは、GetResolvedSelectedDataSourceGetSelectedDataSource という 2 つのメソッドを持っています。「Web フォーム テンプレート データ連結コントロール デザイナのサンプル」で示すこれらのメソッドの実装のコード例を次に示します。

       Function GetResolvedSelectedDataSource() As IEnumerable Implements IDataSourceProvider.GetResolvedSelectedDataSource
          Return CType(CType(Me, IDataSourceProvider).GetSelectedDataSource(), IEnumerable)
       End Function
    
       Function GetSelectedDataSource() As Object Implements IDataSourceProvider.GetSelectedDataSource
          Dim selectedDataSource As Object = Nothing
          Dim dataSource As String = Nothing
    
          Dim binding As DataBinding = DataBindings("DataSource")
          If Not (binding Is Nothing) Then
             dataSource = binding.Expression
          End If
    
          If Not (dataSource Is Nothing) Then
             Dim componentSite As ISite = Component.Site
             If Not (componentSite Is Nothing) Then
                Dim container As IContainer = CType(componentSite.GetService(GetType(IContainer)), IContainer)
    
                If Not (container Is Nothing) Then
                   Dim comp As IComponent = container.Components(dataSource)
                   If TypeOf comp Is IEnumerable Then
                      selectedDataSource = comp
                   End If
                End If
             End If
          End If
    
          Return selectedDataSource
       End Function
    [C#]
    IEnumerable IDataSourceProvider.GetResolvedSelectedDataSource() {
                return (IEnumerable)((IDataSourceProvider)this).GetSelectedDataSource();
            }
    
            object IDataSourceProvider.GetSelectedDataSource() {
                object selectedDataSource = null;
                string dataSource = null;
    
                DataBinding binding = DataBindings["DataSource"];
                if (binding != null) {
                    dataSource = binding.Expression;
                }
    
                if (dataSource != null) {
                    ISite componentSite = Component.Site;
                    if (componentSite != null) {
                        IContainer container = (IContainer)componentSite.GetService(typeof(IContainer));
    
                        if (container != null) {
                            IComponent comp = container.Components[dataSource];
                            if (comp is IEnumerable) {
                                selectedDataSource = comp;
                            }
                        }
                    }
                }
    
                return selectedDataSource;
            }
    

上記の手順を含む完全なサンプルについては、「Web フォーム テンプレート データ連結コントロール デザイナのサンプル」を参照してください。

参照

Web フォーム テンプレート データ連結コントロール デザイナのサンプル | Web フォーム テンプレート エディタの実装 | カスタム デザイナ | Web フォームのデザイン時サポート