效果图:

首先我们去重写一下PropertyGrid:

internal class MyPropertyGrid : System.Windows.Forms.PropertyGrid
{
private System.ComponentModel.Container components = null; public MyPropertyGrid()
{
InitializeComponent();
} protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
} #region Component Designer generated code
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion public void ShowEvents(bool show)
{
ShowEventsButton(show);
}
}

重写一下Container用来封装组件:

class IDEContainer : Container
{
class IDESite : ISite
{
private string name = "";
private IComponent component;
private IDEContainer container; public IDESite(IComponent sitedComponent, IDEContainer site, string aName)
{
component = sitedComponent;
container = site;
name = aName;
} public IComponent Component
{
get { return component; }
}
public IContainer Container
{
get { return container; }
} public bool DesignMode
{
get { return false; }
} public string Name
{
get { return name; }
set { name = value; }
} public object GetService(Type serviceType)
{
return container.GetService(serviceType);
}
} public IDEContainer(IServiceProvider sp)
{
serviceProvider = sp;
} protected override object GetService(Type serviceType)
{
object service = base.GetService(serviceType);
if (service == null)
{
service = serviceProvider.GetService(serviceType);
}
return service;
} public ISite CreateSite(IComponent component)
{
return CreateSite(component, "UNKNOWN_SITE");
} protected override ISite CreateSite(IComponent component, string name)
{
ISite site = base.CreateSite(component, name);
if (site == null)
{
}
return new IDESite(component, this, name);
} private IServiceProvider serviceProvider;
}

去实现EventBindingService接口,用来做事件处理:

public class EventBindingService : System.ComponentModel.Design.EventBindingService
{
public EventBindingService(IServiceProvider myhost)
: base(myhost)
{
} #region IEventBindingService Members
protected override string CreateUniqueMethodName(IComponent component, EventDescriptor e)
{
throw new Exception("The method or operation is not implemented.");
} protected override System.Collections.ICollection GetCompatibleMethods(System.ComponentModel.EventDescriptor e)
{
List<object> l = new List<object>();
return l;
} protected override bool ShowCode(System.ComponentModel.IComponent component, System.ComponentModel.EventDescriptor e, string methodName)
{
throw new Exception("The method or operation is not implemented.");
} protected override bool ShowCode(int lineNumber)
{
throw new Exception("The method or operation is not implemented.");
} protected override bool ShowCode()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
主窗体页面代码(在这里去绑定控件):
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion PropertiesForm propertiesForm;
DesignSurface designSurface; public MainForm()
{
designSurface = new DesignSurface(); propertiesForm = new PropertiesForm(designSurface);
propertiesForm.Show(); IServiceContainer serviceContainer = (IServiceContainer)designSurface.GetService(typeof(IServiceContainer));
serviceContainer.AddService(typeof(IEventBindingService), new EventBindingService(designSurface)); ISelectionService selectionService = (ISelectionService)designSurface.GetService(typeof(ISelectionService));
selectionService.SelectionChanged += new EventHandler(OnSelectionChanged); designSurface.BeginLoad(typeof(Form)); } private void OnSelectionChanged(object sender, System.EventArgs e)
{
ISelectionService s = (ISelectionService)designSurface.GetService(typeof(ISelectionService));
IDesignerHost designerHost = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost)); object[] selection;
if (s.SelectionCount == )
propertiesForm.SetObjectToPropertyGrid(null);
else
{
selection = new object[s.SelectionCount];
s.GetSelectedComponents().CopyTo(selection, );
propertiesForm.SetObjectToPropertyGrid(selection);
}
}

PropertyGrid页面代码(主要为PropertyGrid绑定数据和添加事件选项卡):

private DesignSurface designSurface;
private System.ComponentModel.IContainer components = null;
private MyPropertyGrid pg = null; protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} public PropertiesForm(DesignSurface designSurface)
{
this.designSurface = designSurface; this.SuspendLayout(); pg = new MyPropertyGrid();
pg.Parent = this;
pg.Dock = DockStyle.Fill;
this.ResumeLayout(false);
} internal void SetObjectToPropertyGrid(object[] c)
{
IDesignerHost designerHost = (IDesignerHost)designSurface.GetService(typeof(IDesignerHost));
if (c == null)
pg.SelectedObject = null;
else
pg.SelectedObjects = c; if (designerHost != null)
{
pg.Site = (new IDEContainer(designerHost)).CreateSite(pg);
pg.PropertyTabs.AddTabType(typeof(System.Windows.Forms.Design.EventsTab), PropertyTabScope.Document);
pg.ShowEvents(true);
}
else
{
pg.Site = null;
}
}

想要源码的请加群。

点击加入QQ群:

WPF、AE技术交流群:94234450

 

PropertyGrid仿VS的属性事件窗口的更多相关文章

  1. HTML-全局属性 / 事件属性(转)

    拷贝自:< http://www.runoob.com > HTML 全局属性 New : HTML5 新属性. 属性 描述 accesskey 设置访问元素的键盘快捷键. class 规 ...

  2. DELPHI控件属性事件说明

    常用DELPHI控件属性事件设置说明 常用DELPHI控件属性设置说明目录TForm Class TPanel组件 TToolBar Class TToolButton Class TTimer Cl ...

  3. uniapp仿h5+fire自定义事件触发监听

    仿h5+fire自定义事件触发监听 uni-app调用 event.js 源码记录(点击查看) 1.js下载地址 [event.js](https://ext.dcloud.net.cn/plugin ...

  4. WPF 学习笔记-设置属性使窗口不可改变大小

    原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...

  5. C# 属性事件一些设置说明

    大致列举一些常用的属性或事件的一些修饰 用法类似,主要是对属性的进一步设置 [Browsable(true)] public bool Enable {get;set;} 顺便说一下事件的应用: pu ...

  6. js 中HTML的 onkeycode 和onkeydown属性事件

    <!DOCTYPE html><html><head><script>function displayResult(){var x=document.g ...

  7. jQuery的属性,事件及操作

    1.属性操作 1.1 基本属性操作 $("img").attr("src") 返回文档中所有图像的src属性值 $("img").attr( ...

  8. Delphi7.0常用函数-属性-事件

    abort 函数 引起放弃的意外处理 addexitproc 函数 将一过程添加到运行时库的结束过程表中 addr 函数 返回指定对象的地址 adjustlinebreaks 函数 将给定字符串的行分 ...

  9. jQuery Colorpicker Spectrum api 中文 文档 属性 事件 方法

    jQuery Colorpicker Spectrum 所需的CSS和JavaScript文件: <script src='spectrum.js'></script> < ...

随机推荐

  1. 事件tou

    #define EV_TIMER_RESOLUTION 1 /* 1 msec */ #define EV_READ_EVENT EPOLLIN #define EV_WRITE_EVENT EPOL ...

  2. 移动周报:十款最实用的Android UI设计工具

    上一周可以说是一个不断Mark周,从最实用的Android UI设计工具.免费移动应用测试框架推荐,到HTML5开发框架等等,各种开发工具.框架精彩丰呈,看得小伙伴们是不亦乐乎.当然,还有不容错过的M ...

  3. JS 日期操作类

    /* 日期对象格式化为指定日期格式 */Date.prototype.format = function (format) { var o = { "M+": this.getMo ...

  4. java学习之创建线程方法二

    我们上一节当中讲到了创建线程的第一种方法,就是继承Thread类,覆写Thread当中的run方法,然后创建子类对象,之后调用对象的start方法启动线程.但是这种方法有一个缺陷,因为我们知道在jav ...

  5. AC-BM算法原理与代码实现(模式匹配)

    AC-BM算法原理与代码实现(模式匹配) AC-BM算法将待匹配的字符串集合转换为一个类似于Aho-Corasick算法的树状有限状态自动机,但构建时不是基于字符串的后缀而是前缀.匹配 时,采取自后向 ...

  6. DevExpress中XtraGrid控件对GridView每行的颜色设置 zt

    改变行颜色 private void GridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArg ...

  7. poj magic number

    Problem H Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  8. JavaScript高级程序设计22.pdf

    操作节点 appendChild()用于向childNodes列表末尾添加一个节点,appendChild()返回新增的节点 var returnedNode=someNode.appendChild ...

  9. C语言调用汇编实现字符串对换

    1. 前面配置arm交叉编译环境. 2. 配置好qemu-arm C语言代码string-switch.c: #include <stdio.h> #include <stdlib. ...

  10. C++ 把输出结果写入文件/从文件中读取数据

    先包含头文文件 #include<fstream> 输出到文件 ofstream fout;  //声明一个输出流对象 fout.open("output.txt"); ...