第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要從 .NET Winforms 控件的 PropertyGrid 中隱藏僅設(shè)計器屬性

需要從 .NET Winforms 控件的 PropertyGrid 中隱藏僅設(shè)計器屬性

PHP
富國滬深 2024-01-20 21:38:14
我深入研究在我的 C#/.NET 解決方案中使用 Winforms 設(shè)計器(System.ComponentModel.Design 命名空間),以便我的用戶可以在我正在運行的應(yīng)用程序中訪問表單設(shè)計器。其中大部分工作正常,但我遇到了一個非常具體的問題:我遇到了 Microsoft 控件上的一個屬性,該屬性僅在設(shè)計時出現(xiàn),即針對該控件的設(shè)計時實例。我想抑制該屬性,以便用戶在將該控件的實例放置在我的程序的 Winform 設(shè)計圖面實現(xiàn)上時無法修改它。詳細(xì)信息:當(dāng)用戶將控件從工具箱拖放到設(shè)計器表面時,我確保選擇新添加的控件的設(shè)計器實例(以便它顯示調(diào)整大小手柄,并且屬性網(wǎng)格顯示該控件的設(shè)計-時間屬性)。我通過使用選擇服務(wù)的GetSelectedComponents()方法并將屬性網(wǎng)格的SelectedObjects屬性分配給結(jié)果,將設(shè)計器表面上的選定對象綁定到屬性網(wǎng)格。我的工具箱中的許多控件都是 .NET 控件。其中之一是 .NET Winforms TableLayoutPanel控件。當(dāng)您將該控件的實例放置在設(shè)計器圖面上時,您將在綁定的PropertyGrid中看到Columns屬性。我想禁止該屬性,以便它不會出現(xiàn)在PropertyGrid中。問題在于, TableLayoutPanel類型的屬性列表中似乎不存在此 Columns 屬性,因此,例如, usingselectedComponents[0].GetType().GetProperties()不包含Columns屬性。另外,我無法為現(xiàn)有的Columns屬性創(chuàng)建新屬性或覆蓋它,因為它在設(shè)計時不會顯示為TableLayoutPanelBrowsable(false)控件的公開屬性,因此我無法使用該屬性來裝飾它。我似乎無法利用PreFilterProperties或PostFilterProperties因為我無法交互和自定義TableLayoutPanel 的設(shè)計器。如何抑制TableLayoutPanel的Columns設(shè)計器屬性,使其不會出現(xiàn)在PropertyGrid中,而無需編寫自己的設(shè)計器?
查看完整描述

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以及分配給編輯/添加/刪除列和行的動詞。


查看完整回答
反對 回復(fù) 2024-01-20
  • 1 回答
  • 0 關(guān)注
  • 277 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號