⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 behaviordesigneractionlist.cs

📁 中文名:Windows Forms 程序设计 英文名:Windows Forms Programming in c# 作者: Chris Sells 翻译: 荣耀 蒋贤哲 出版社:人民
💻 CS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -