PropertyGrid仿VS的屬性事件窗口


效果圖:

首先我們去重寫一下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 == 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  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM