behaviordesigneractionlist.cs

来自「中文名:Windows Forms 程序设计 英文名:Windows Form」· CS 代码 · 共 88 行

CS
88
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace AlarmClockControlLibrary {
  public class BehaviorDesignerActionList : DesignerActionList {

    public BehaviorDesignerActionList(IComponent component) : base(component) { }

    public override DesignerActionItemCollection GetSortedActionItems() {

      if( this.Component == null ) return null;
      
      // Create list to store designer action items
      DesignerActionItemCollection actionItems = new DesignerActionItemCollection();

      // Fill list of designer action items

      actionItems.Add(new DesignerActionHeaderItem("Behavior"));

      actionItems.Add(
        new DesignerActionTextItem(
        "Properties that affect how the AlarmClockControl behaves.",
        "Behavior"));

      // Add BackupAlarm designer action property item
      actionItems.Add(
        new DesignerActionPropertyItem(
          "BackupAlarm",
          "Backup Alarm",
          this.GetAttributeString<CategoryAttribute>(this.AlarmClockControl, "BackupAlarm", "Category"),
          this.GetAttributeString<DescriptionAttribute>(this.AlarmClockControl, "BackupAlarm", "Description")));

      // Add PrimaryAlarm designer action property item
      actionItems.Add(
        new DesignerActionPropertyItem(
          "PrimaryAlarm",
          "Primary Alarm",
          this.GetAttributeString<CategoryAttribute>(this.AlarmClockControl, "PrimaryAlarm", "Category"),
          this.GetAttributeString<DescriptionAttribute>(this.AlarmClockControl, "PrimaryAlarm", "Description")));
          
      // Return list of designer action items
      return actionItems;
    }

    // BackupAlarm proxy property
    public DateTime BackupAlarm {
      get { return this.AlarmClockControl.BackupAlarm; }
      set { SetProperty("BackupAlarm", value); }
    }

    // PrimaryAlarm proxy property
    public DateTime PrimaryAlarm {
      get { return this.AlarmClockControl.PrimaryAlarm; }
      set { SetProperty("PrimaryAlarm", value); }
    }

    // Helper method to safely set a component抯 property
    private void SetProperty(string propertyName, object value) {
      // Get property
      PropertyDescriptor property = TypeDescriptor.GetProperties(this.AlarmClockControl)[propertyName];
      // Set property value
      property.SetValue(this.AlarmClockControl, value);
    }

    // Helper property to acquire a AlarmClockControl reference
    private AlarmClockControl AlarmClockControl {
      get { return (AlarmClockControl)this.Component; }
    }
    
    // Helper method that returns the category string from a CategoryAttribute 
    // assigned to a property exposed by the specified objecs
    private string GetAttributeString<T>(object source, string sourceProperty, string attributeProperty) {
      PropertyInfo sourcePropertyInfo = source.GetType().GetProperty(sourceProperty);
      T attribute = (T)sourcePropertyInfo.GetCustomAttributes(typeof(T), false)[0];
      if( attribute == null ) return null;
      Type attributeType = attribute.GetType();
      PropertyInfo attributePropertyInfo = attributeType.GetProperty(attributeProperty);
      return (string)attributePropertyInfo.GetValue(attribute, null);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?