博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PropertyGrid仿VS的属性事件窗口
阅读量:6577 次
发布时间:2019-06-24

本文共 7077 字,大约阅读时间需要 23 分钟。

效果图:

首先我们去重写一下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 l = new List();            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 == 0)                propertiesForm.SetObjectToPropertyGrid(null);            else            {                selection = new object[s.SelectionCount];                s.GetSelectedComponents().CopyTo(selection, 0);                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  

 

转载于:https://www.cnblogs.com/BeiJing-Net-DaiDai/p/3254455.html

你可能感兴趣的文章
23种设计模式(1):单例模式
查看>>
socket 编程入门教程(五)UDP原理:4、“有连接”的UDP
查看>>
Jquery获取iframe中的元素
查看>>
Laravel 学习笔记5.3之 Query Builder 源码解析(下)
查看>>
Struts2简单入门实例
查看>>
2012CSDN年度博客之星评选http://vote.blog.csdn.net/item/blogstar/xyz_lmn
查看>>
BZOJ 4037 [HAOI2015]数字串拆分 ——动态规划
查看>>
Craking the Interview-1
查看>>
POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
查看>>
CCF NOI1150 确定进制
查看>>
SpringBoot实战总汇--详解
查看>>
2018年7月1日笔记
查看>>
尝试使用iReport4.7(基于Ubuntu Desktop 12.04 LTS)
查看>>
安装GIT(基于Ubuntu Desktop 12.04 LTS)
查看>>
动态规划:金矿模型
查看>>
子元素应该margin-top为何会影响父元素【转】
查看>>
AJAX 状态值(readyState)与状态码(status)详解
查看>>
BZOJ3668:[NOI2014]起床困难综合症(贪心)
查看>>
jQuery 中bind(),live(),delegate(),on() 区别
查看>>
C++编程中const和#define的区别
查看>>