效果图:

首先我们去重写一下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. JQuery replace 替换全部

    天在做写个程序时遇到需要替换的功能,可是一开始用jquery的replace时,发现只替换到第一个.最后没办法,只好用正则表达式来例如下面   re = new RegExp("{thisc ...

  2. Wordpress 网站搭建及性能监控方法详解!

    前言 说到 Wordpress,大家往往想到的是博客,其实,如今的 WordPress 已经成为全球使用量最多的开源 CMS 系统.并且,如果你有一定的技术基础稍加改动,就可以搭建出新闻网站.企业网站 ...

  3. 安卓开发中,什么样的功能适合抽取成 Library?

    我们都知道如果将所有的功能都写成 Library,那么我们在编写应用程序的时候就可以快速便捷的写出想要的功能,因为这些已经事先都实现过了,这样在写代码的时候就可以迅速的将 Library 依赖到我们的 ...

  4. Java 构造器 一道构造器调用子类重载方法的题目

    构造器中不能new本类对象,否则进入死循环. 构造器没有返回值,也没有void修饰. 使用关键字super可以调用父类的构造器,而且这一句必须放在第一句的位置,否则无法编译. 题目: 请写出以下程序的 ...

  5. SPRING IN ACTION 第4版笔记-第三章Advancing wiring-001-DataSource在应用和开发环境之间切换 profile

    一. DataSource在应用和开发环境的产生方式不同,可以用srping 的profile管理 Spring’s solution for environment-specific beans i ...

  6. [QuickX]xcode运行Quick-cocos2d-x项目时自动更新lua资源文件

    1.项目设置 build settings ->build options ->Scan all source files and Includes = YES 2.加入script (1 ...

  7. Specify a culture in string conversion explicitly

    Specify a culture in string conversion explicitly There are different methods of grouping symbols, l ...

  8. JS思维导图

  9. (转载)http_build_query用法,挺方便的

    (转载)http://www.cnblogs.com/zhja/archive/2012/11/10/2764174.html http_build_query (PHP 5) http_build_ ...

  10. SQL Server数据库修改字段属性

      1:向表中添加字段 Alter table [表名] add [列名] 类型 2: 删除字段 Alter table [表名] drop column [列名] 3: 修改表中字段类型 (可以修改 ...