效果图:

首先我们去重写一下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. Painting The Wall 期望DP Codeforces 398_B

    B. Painting The Wall time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Codeforces Round #198 (Div. 2) —— A

    最水的题,可惜当时赶时间没有注意数据范围:暴力超时了! 其实应该用x,y的最大公约数来判断: 代码: #include<iostream> using namespace std; int ...

  3. Ecmall系统自带的分页功能

    在Ecmall的二次开发中,分页是必不可少的.这个系统已经自带了分页功能,下面来看看如何使用这个分页. 下面是一个自定义的类,用于查看订单的详细情况.关键在于get_order_data()这个方法, ...

  4. 如何解决C#编译中"csc不是内部或外部命令"的问题

    安装完 VisualStudio 2010编译环境后,是不能用命令行直接编译写好的csc文件的,如果不配置环境变量,在命令提示符(cmd)中编译扩展名为cs的文件,会出现错误提示“csc不是内部或外部 ...

  5. UML 中关系详解以及在visio中的表示

    http://www.cnblogs.com/kittywei/archive/2013/05/15/3079536.html

  6. 最新ps cs6序列号 永久免费可用

    一. 序列号 除非是从官方购买,从其它任何途径得到的序列号(包括网上流传的注册机生成的)都是不能通过联网验证的,必须使用破解补丁,或是通过修改hosts文件的方式来激活.因此,除了正版,不存在所谓的“ ...

  7. 【HDOJ】1198 Farm Irrigation

    其实就是并查集,写麻烦了,同样的代码第一次提交wa了,第二次就过了. #include <stdio.h> #include <string.h> #define MAXNUM ...

  8. BZOJ_1036_[ZJOI2008]_树的统计Conut_(树链剖分)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1036 给出一棵树以及各点的权值,对数进行如下三种操作: 1.改变某一节点u的值为t; 2.求节 ...

  9. HIbernate java.lang.AbstractMethodError: com.microsoft.jdbc.base.BaseDatabaseMetaData.supportsGetGeneratedKeys()Z

    [HIbernate]java.lang.AbstractMethodError: com.microsoft.jdbc.base.BaseDatabaseMetaData.supportsGetGe ...

  10. Ubuntu---2

    1.Ubuntu学习笔记之:安装中文语言包 http://askubuntu.com/questions/59356/how-do-i-get-chinese-input-to-work