http://msdn.microsoft.com/zh-cn/library/tbt775x3(v=vs.100).aspx

元数据筛选允许设计器在设计时修改组件或控件所公开的属性、特性和事件。

Visible 属性,以后再恢复此属性的运行时值。

ITypeDescriptorFilterService 实现。

ITypeDescriptorFilterService 的方法随后都会被调用,这样,服务就可以随意筛选特性、事件和属性。

TypeDescriptor 的方法。

IDesignerFilter 接口定义了一组能够在设计器中重写和实现的方法,以更改设计器所管理的组件在设计时公开的属性、事件或特性。

以“Pre”开头的方法在紧靠名称以“Post”开头的方法之前调用。

PostFilterAttributes 方法的重写。

PostFilterEvents 方法的重写。

PostFilterProperties 方法的重写。

注意

IDesignerFilter 的设计器进行扩展时,每个 PostMethodName 方法应在更改自己的特性之后调用基类中相应的 PostMethodName 方法,每个PreMethodName 方法应在更改自己的特性之前调用基类中相应的 PreMethodName 方法。

IDesignerFilter 接口的方法签名。

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);
}
下面的代码示例演示 IDesignerFilter 的实现,这一实现将设计器的 Color 属性添加到关联的组件中。需要添加对 System.Design.dll 的引用。
C#VB
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()
      {}
   }
}

Windows 窗体设计器示例。

AddService 方法。

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));
}

 

相关文章:

  • 2021-07-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-05
  • 2022-12-23
  • 2022-02-22
  • 2022-02-23
  • 2021-11-04
  • 2021-05-12
  • 2022-12-23
相关资源
相似解决方案