效果图:

首先我们去重写一下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. 为什么 API 监控对于任何业务来说都重要?

    对于商务运算来说一个比较稳定的趋势在于对 API 日渐增长的依赖性,几乎每一个代码级交互过程都会调用 API 来收集数据或触发某些关键过程.没有 API ,你将无法与同伴进行文件交流,没有 API , ...

  2. 【网络流24题】No.4 魔术球问题 (二分+最小路径覆盖)

    [题意] 假设有 n 根柱子, 现要按下述规则在这 n 根柱子中依次放入编号为 1, 2, 3, ¼的球.( 1)每次只能在某根柱子的最上面放球.( 2)在同一根柱子中,任何 2 个相邻球的编号之和为 ...

  3. Android 获取SDCard中某个目录下图片

    本文介绍Android开发中如何获取SDCard中某目录下的所有图片并显示出来,下面的我们提供的这个函数是通用的,只要提供路径就可以查询出该目录下所有图片的路径信息,并保存到一个List<Str ...

  4. bzoj1093

    首先缩点然后半连通其实就是缩点后节点数最多的链注意这里一定是一条链才一定是半连通然后重建图拓扑排序上做dp即可 type node=record po,next:longint; end; ..] o ...

  5. UIImagePickerController 如何显示中文界面

    1: 添加中文的 Localizations Project --> Info --> Localizations  添加 "Chinese(Simplified)" ...

  6. (转载)Chrome 快捷键 整理版

    (转载)http://www.douban.com/group/topic/13802153/ chrome窗口和标签页快捷键: Ctrl+N 打开新窗口 Ctrl+T 打开新标签页 Ctrl+Shi ...

  7. Climbing Worm

    Climbing Worm Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. Oracle statspack 安装及使用

    转载:http://blog.csdn.net/joinplay/article/details/23358133 oracle statspack 工具从oracle 8.1.6开始被引用,从ora ...

  9. Ubuntu 下安装Kibana和logstash

    原文地址:http://www.cnblogs.com/saintaxl/p/3946667.html 简单来讲他具体的工作流程就是 logstash agent 监控并过滤日志,将过滤后的日志内容发 ...

  10. .net常見面試題(三)

    1, 请你说说.NET中类和结构的区别? 答:结构和类具有大体的语法,但是结构受到的限制比类要多.结构不能申明有默认的构造函数,为结构的副本是又编译器创建和销毁的,所以不需要默认的构造函数和析构函数. ...