1 回答

TA貢獻(xiàn)1811條經(jīng)驗 獲得超4個贊
如果您試圖避免TableLayoutPanelDesigner
自己編寫,那么我可以建議您使用以下解決方法。
ITypeDescriptorFilterService
是負(fù)責(zé)調(diào)用PreFilterProperties
設(shè)計器方法的接口。該類DesignSurface
在其ServiceContainer
.?因此,您可以刪除現(xiàn)有的已注冊實例并注冊您自己的ITypeDescriptorFilterService
.
在新的實現(xiàn)中,重寫FilterProperties
并執(zhí)行您認(rèn)為合適的任何操作,例如您可以檢查組件的類型是否為TableLayoutPanel
,然后不要調(diào)用其設(shè)計器PreFilterProperties
。
然后,為了總結(jié)解決方案,您需要通過從DesignSurface
類派生并刪除已注冊ITypeDescriptorFilterService
并注冊您創(chuàng)建的所需實例來創(chuàng)建自己的設(shè)計界面。
例子
僅供您參考,您正在查找的屬性的名稱是ColumnStyles
,并且默認(rèn)情況下具有Browsable(false)
屬性。但默認(rèn)的設(shè)計者TableLayoutPanel
用可瀏覽的版本替換了這個屬性。
我在這個例子中所做的就是阻止設(shè)計者使這些屬性可見。
首先提供ITypeDescriptorFilterService
.?下面的一個實際上是System.Design
匯編中的現(xiàn)有實現(xiàn),我更改了它的FilterProperties
方法并檢查了組件是否是TableLayoutPanel
,我要求什么也不做。
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
public class TypeDescriptorFilterService : ITypeDescriptorFilterService
{
? ? internal TypeDescriptorFilterService()
? ? {
? ? }
? ? private IDesigner GetDesigner(IComponent component)
? ? {
? ? ? ? ISite site = component.Site;
? ? ? ? if (site != null)
? ? ? ? {
? ? ? ? ? ? IDesignerHost service = site.GetService(typeof(IDesignerHost)) as IDesignerHost;
? ? ? ? ? ? if (service != null)
? ? ? ? ? ? ? ? return service.GetDesigner(component);
? ? ? ? }
? ? ? ? return (IDesigner)null;
? ? }
? ? bool ITypeDescriptorFilterService.FilterAttributes(IComponent component, IDictionary attributes)
? ? {
? ? ? ? if (component == null)
? ? ? ? ? ? throw new ArgumentNullException("component");
? ? ? ? if (attributes == null)
? ? ? ? ? ? throw new ArgumentNullException("attributes");
? ? ? ? IDesigner designer = this.GetDesigner(component);
? ? ? ? if (designer is IDesignerFilter)
? ? ? ? {
? ? ? ? ? ? ((IDesignerFilter)designer).PreFilterAttributes(attributes);
? ? ? ? ? ? ((IDesignerFilter)designer).PostFilterAttributes(attributes);
? ? ? ? }
? ? ? ? return designer != null;
? ? }
? ? bool ITypeDescriptorFilterService.FilterEvents(IComponent component, IDictionary events)
? ? {
? ? ? ? if (component == null)
? ? ? ? ? ? throw new ArgumentNullException("component");
? ? ? ? if (events == null)
? ? ? ? ? ? throw new ArgumentNullException("events");
? ? ? ? IDesigner designer = this.GetDesigner(component);
? ? ? ? if (designer is IDesignerFilter)
? ? ? ? {
? ? ? ? ? ? ((IDesignerFilter)designer).PreFilterEvents(events);
? ? ? ? ? ? ((IDesignerFilter)designer).PostFilterEvents(events);
? ? ? ? }
? ? ? ? return designer != null;
? ? }
? ? bool ITypeDescriptorFilterService.FilterProperties(IComponent component, IDictionary properties)
? ? {
? ? ? ? if (component == null)
? ? ? ? ? ? throw new ArgumentNullException("component");
? ? ? ? if (properties == null)
? ? ? ? ? ? throw new ArgumentNullException("properties");
? ? ? ? if (typeof(TableLayoutPanel).IsAssignableFrom(component.GetType()))
? ? ? ? ? ? return true;
? ? ? ? IDesigner designer = this.GetDesigner(component);
? ? ? ? if (designer is IDesignerFilter)
? ? ? ? {
? ? ? ? ? ? ((IDesignerFilter)designer).PreFilterProperties(properties);
? ? ? ? ? ? ((IDesignerFilter)designer).PostFilterProperties(properties);
? ? ? ? }
? ? ? ? return designer != null;
? ? }
}
然后通過派生創(chuàng)建一個設(shè)計圖面DesignSurface:
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
public class MyDesignSurface : DesignSurface
{
? ? public MyDesignSurface() : base()
? ? {
? ? ? ? this.ServiceContainer.RemoveService(typeof(ITypeDescriptorFilterService));
? ? ? ? this.ServiceContainer.AddService(typeof(ITypeDescriptorFilterService), new TypeDescriptorFilterService());
? ? }
}
然后例如以這種方式初始化設(shè)計圖面:
public partial class Form1 : Form
{
? ? public Form1()
? ? {
? ? ? ? InitializeComponent();
? ? ? ? var surface = new MyDesignSurface();
? ? ? ? var host = (IDesignerHost)surface.GetService(typeof(IDesignerHost));
? ? ? ? var selectionService = (ISelectionService)surface.GetService(typeof(ISelectionService));
? ? ? ? surface.BeginLoad(typeof(Form));
? ? ? ? var root = (Form)host.RootComponent;
? ? ? ? var tableLayoutPanel1 = (Control)host.CreateComponent(typeof(TableLayoutPanel), "tableLayoutPanel1");
? ? ? ? root.Controls.Add(tableLayoutPanel1);
? ? ? ? var view = (Control)surface.View;
? ? ? ? view.Dock = DockStyle.Fill;
? ? ? ? this.Controls.Add(view);
? ? ? ? selectionService.SetSelectedComponents(new[] { tableLayoutPanel1 });
? ? ? ? var propertyGrid1 = new PropertyGrid() { Dock = DockStyle.Right, Width = 200 };
? ? ? ? this.Controls.Add(propertyGrid1);
? ? ? ? propertyGrid1.SelectedObjects = selectionService.GetSelectedComponents().Cast<object>().ToArray();
? ? }
}
然后運行您的設(shè)計器應(yīng)用程序,您將看到Columns屬性Rows按預(yù)期隱藏。
您需要隱藏ColumnCount屬性RowCount以及分配給編輯/添加/刪除列和行的動詞。
- 1 回答
- 0 關(guān)注
- 277 瀏覽
添加回答
舉報