メタデータ フィルター処理

メタデータ フィルター処理によって、デザイナーは、デザイン時にコンポーネントやコントロールによって公開されるプロパティ、属性、およびイベントのセットを変更できます。

たとえば、Control には、コントロールを表示するかどうかを指定する Visible というプロパティがあります。 ただし、デザイン時には、開発者がデザイン サーフェイスにコントロールを配置できるように、このプロパティの値に関係なくコントロールが表示された方が便利です。 Control のデザイナーは、デザイン時に Visible プロパティをデザイナー独自のプロパティに置き換え、デザイン完了後にこのプロパティの実行時の値を復元します。

メタデータ フィルター処理を実行するために、デザイナーは IDesignerFilter インターフェイスを実装するか、またはデザイン時サービス プロバイダーに、デザイン時環境で任意のコンポーネントに対してメタデータ フィルター処理を実行できる ITypeDescriptorFilterService の実装を追加できます。

デザイン時にコンポーネントを選択すると、プロパティ ブラウザーは TypeDescriptor のメソッドを使用して、コンポーネントに属性、イベント、およびプロパティを照会します。 デザイン モードでコンポーネントの属性、イベント、およびプロパティが照会されたとき、IDesignerFilter インターフェイスを実装するコンポーネントのデザイナーは、そのコンポーネントによって返される属性、イベント、およびプロパティのセットを変更できます。 次に、アクティブな ITypeDescriptorFilterService のメソッドが呼び出され、属性、イベント、およびプロパティのフィルター処理をサービスが実行できるようになります。

デザイン モードのコンポーネントの属性、イベント、およびプロパティが照会されるのは、通常、コンポーネントの RefreshTypeDescriptor メソッドが呼び出されたとき、[プロパティ] ウィンドウが更新されたとき、デザイン モードが確立または再確立されたとき、および主要選択が設定されたときです。 それ以外に、他のオブジェクトのメソッドやデザイン時環境から TypeDescriptor のメソッドが呼び出される場合もあります。

コンポーネントのメタデータ フィルター処理のための IDesignerFilter インターフェイス

IDesignerFilter インターフェイスは、デザイナーでオーバーライドしたり実装したりできるメソッドのセットを定義します。これらのメソッドによって、デザイン時にデザイナーで管理されるコンポーネントが公開するプロパティ、イベント、または属性を変更できます。

IDesignerFilter インターフェイスの各メソッドの名前には、"Pre" または "Post" のいずれかのプレフィックスが付きます。 また、各メソッドの名前には、追加、変更、または削除できるメンバーの種類に応じて、"Attributes"、"Events"、または "Properties" のいずれかのサフィックスが付きます。 属性、イベント、またはプロパティを追加するには、名前が "Pre" で始まる関連するメソッドを使用します。 属性、イベント、またはプロパティを変更または削除するには、名前が "Post" で始まる関連するメソッドを使用します。 名前が "Pre" で始まるメソッドは、名前が "Post" で始まるメソッドの直前に呼び出されます。

属性を追加する場合は、PreFilterAttributes メソッドのオーバーライドを実装して、メソッドに渡される IDictionary に新しい Attribute を追加します。 ディクショナリ内のキーは属性の型 ID です。 属性を変更または削除するには、PostFilterAttributes メソッドのオーバーライドを実装します。

イベントを追加する場合は、PreFilterEvents メソッドのオーバーライドを実装して、メソッドに渡される IDictionary に新しい EventDescriptor を追加します。 ディクショナリ内のキーはイベントの名前です。 イベントを変更または削除するには、PostFilterEvents メソッドのオーバーライドを実装します。

プロパティを追加する場合は、PreFilterProperties メソッドのオーバーライドを実装して、メソッドに渡される IDictionary に新しい PropertyDescriptor を追加します。 ディクショナリ内のキーはプロパティの名前です。 プロパティを変更または削除するには、PostFilterProperties メソッドのオーバーライドを実装します。

注意

IDesignerFilter を実装するデザイナーをクラスが拡張するときには、各 PostMethodName メソッドがその属性を変更した後で、基本クラスの対応する PostMethodName メソッドを呼び出す必要があります。また、各 PreMethodName メソッドがその属性を変更する前に、基本クラスの対応する PreMethodName メソッドを呼び出す必要もあります。

IDesignerFilter インターフェイスのメソッド シグネチャを次のコード例に示します。

Public Interface IDesignerFilter
   Sub PostFilterAttributes(attributes As IDictionary)
   Sub PostFilterEvents(events As IDictionary)
   Sub PostFilterProperties(properties As IDictionary)
   Sub PreFilterAttributes(attributes As IDictionary)
   Sub PreFilterEvents(events As IDictionary)
   Sub PreFilterProperties(properties As IDictionary)
End Interface
public interface IDesignerFilter {
    void PostFilterAttributes(IDictionary attributes);
    void PostFilterEvents(IDictionary events);
    void PostFilterProperties(IDictionary properties);
    void PreFilterAttributes(IDictionary attributes);
    void PreFilterEvents(IDictionary events);
    void PreFilterProperties(IDictionary properties);
}

デザイナーの Color プロパティを、関連付けられたコンポーネントに追加する IDesignerFilter の実装を次のコード例に示します。 System.Design.dll への参照を追加する必要があります。

Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

Namespace IDesignerFilterSample
 _
    Public Class DesignerFilterDesigner
        Inherits ComponentDesigner        

        ' Designer color property to add to component.
        Public Property TestColor() As Color
            Get
                Return Me.intcolor
            End Get
            Set(ByVal Value As Color)
                Me.intcolor = Value
            End Set
        End Property

        ' Color for TestColor property.
        Private intcolor As Color = Color.Azure

        Public Function DesignerFilterDesigner()
        End Function 'DesignerFilterDesigner

        ' Adds a color property of this designer to the component.
        Protected Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)
            MyBase.PreFilterProperties(properties)
            ' Adds a test property to the component.
            properties.Add("TestColor", TypeDescriptor.CreateProperty(GetType(DesignerFilterDesigner), "TestColor", GetType(System.Drawing.Color), Nothing))
        End Sub 'PreFilterProperties

    End Class 'DesignerFilterDesigner

    ' Component with which the DesignerFilterDesigner is associated.
    <Designer(GetType(DesignerFilterDesigner))>  _    
    Public Class TestComponent
        Inherits Component

        Public Function TestComponent()
        End Function 'TestComponent
    End Class 'TestComponent

End Namespace
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace IDesignerFilterSample
{
   public class DesignerFilterDesigner : ComponentDesigner, IDesignerFilter
   {
      // Designer color property to add to component.
      public Color TestColor
      {
         get
         { return this.intcolor;   }
         set
         { this.intcolor = value; }
      }

      // Color for TestColor property.
      private Color intcolor = Color.Azure;

      public DesignerFilterDesigner()
      {}

      // Adds a color property of this designer to the component.
      protected override void PreFilterProperties(System.Collections.IDictionary properties)
      {
         base.PreFilterProperties(properties);
         // Adds a test property to the component.
         properties.Add("TestColor", TypeDescriptor.CreateProperty(typeof(DesignerFilterDesigner), "TestColor", typeof(System.Drawing.Color), null));
      }
   }

   // Component with which the DesignerFilterDesigner is associated.
   [Designer(typeof(DesignerFilterDesigner))]
   public class TestComponent : Component
   {
      public TestComponent()
      {}
   }
}

IDesignerFilter インターフェイスを使用してプロパティのフィルター処理を実装した Windows フォーム コントロール デザイナーの例については、「Windows フォーム デザイナーのサンプル」を参照してください。

グローバル デザイン モードのメタデータ フィルター処理のための ITypeDescriptorFilterService

デザイン時プロジェクトの任意のコンポーネントにメタデータ フィルター処理を提供するには、デザイン時にサービスを提供するサービス プロバイダーに ITypeDescriptorFilterService の実装を追加し、デザイン モードの ComponentSite プロパティから返される ISite を取得して、それに実装されている IServiceContainer インターフェイスの AddService メソッドを使用します。

アプリケーションに ExampleFilterService という ITypeDescriptorFilterService サービスを追加する方法を次のコード例に示します。

IDesignerHost dh = (IDesignerHost)this.Component.GetService(typeof(IDesignerHost));
if( dh != null )
{
   // First gets any previous ITypeDescriptorFilterService to replace when 
   // the current service is removed, and to call if the new service 
   // implements service chaining.
   ITypeDescriptorFilterService itdfs = 
   (ITypeDescriptorFilterService)dh.GetService(    typeof(ITypeDescriptorFilterService));
   
   oldService = (ITypeDescriptorFilterService)dh.GetService(
   typeof(ITypeDescriptorFilterService));
         
   if(oldService != null)
      // Removes any old ITypeDescriptorFilterService.
      dh.RemoveService(typeof(ITypeDescriptorFilterService));
         
   // Adds an ExampleFilterService that implements service chaining.
   dh.AddService(typeof(ITypeDescriptorFilterService), 
   new ExampleFilterService(oldService));
}

ITypeDescriptorFilterService の実装例については、ITypeDescriptorFilterService クラスのリファレンス ドキュメントを参照してください。

参照

処理手順

方法 : デザイン モードでコンポーネントの属性、イベント、およびプロパティを調整する

概念

基本デザイナー クラス

デザイナー動詞

方法 : コントロール用デザイナーを実装する

型記述子の概要

その他の技術情報

カスタム デザイナー