[英]Expose A Property Of Type List<class> In VS Designer Limiting/Hiding Access To Members Or Show Property As Expandable Menu?
我為我的Windows應用程序創建了一個自定義選項卡控件。 自定義選項卡控件擴展System.Windows.Forms.TabControl。 我之所以創建自定義選項卡控件,是因為我可以在Visual Studio屬性窗口中公開一個屬性,該屬性允許我為自定義選項卡控件中的每個選項卡頁面定義單獨的字體。 以下是類定義的快速瀏覽:
[ToolboxItem(true)]
public partial class CustomTabControl : System.Windows.Forms.TabControl
為了存儲每個單獨的名稱/字體對,我在CustomTabControl中創建了一個嵌套類:
[TypeConverter(typeof(TabFontConverter))]
public class TabFont
{
public string Name { get; set; }
public Font Font { get; set; }
public TabFont()
{
}
public TabFont(string name, Font font)
{
this.Name = name;
this.Font = font;
}
}
(注意在TabFont類上面使用TypeConverter屬性。我添加了這個,因為我在網上讀過這個,如果我要在Visual Studio設計器中公開這個類型,這是必需的。)
這是轉換器類(它也嵌套在CustomTabControl中):
public class TabFontConverter : TypeConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] filter)
{
return TypeDescriptor.GetProperties(value, filter);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}
我定義了一個類變量來將自定義選項卡字體存儲為List:
private List<TabFont> _CustomTabFonts = new List<TabFont>();
為了填充此列表,我為ControlAdded添加了一個事件處理程序:
this.ControlAdded += new ControlEventHandler(CustomTabControl_ControlAdded);
以下是我在事件處理程序中填充列表的方法:
private void CustomTabControl_ControlAdded(object sender, ControlEventArgs e)
{
if (e.Control.GetType() == typeof(TabPage))
{
TabPage newTabPage = (TabPage)e.Control;
Font newTabPageFont = newTabPage.Font;
_CustomTabFonts.Add(new TabFont(newTabPage.Text, newTabPageFont));
e.Control.Font = newTabPageFont;
}
}
最后,為了完成這一切,我定義了以下代碼,允許Visual Studio設計器訪問/修改自定義選項卡字體列表:
[DefaultValue(typeof(List<TabFont>))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public List<TabFont> CustomTabFonts
{
get { return _CustomTabFonts; }
set { _CustomTabFonts = value; }
}
重建后,我切換回Visual Studio設計視圖,通過從工具箱中拖動一個CustomTabControl到我的主窗體。然后我添加了兩個名為“Tab 1”和“Tab 2”的標簽頁。
這是我的自定義選項卡字體屬性的屬性框顯示的內容:
請注意,它將類型顯示為集合,並提供單擊以進行編輯的按鈕。 當我點擊這里的按鈕時,我看到的是:
關於屬性編輯器,我有幾個問題。
屬性編輯器的右側顯示所選選項卡的字體和名稱。 我只希望能夠更改字體,而不是名稱。 如何隱藏名稱字段或至少使其只讀? (我更希望名字字段不顯示在那里,因為我不想更改它而且它也是多余的,因為名稱已經顯示在屬性編輯器的左側。)
屬性編輯器的左側顯示了標簽列表,這正是我想要的。 但是,我不想允許移動,添加或刪除任何這些成員。 如何隱藏或禁用移動(向上/向下箭頭)和添加/刪除按鈕?
屬性編輯器的左側有一個名為“Members”的標題。 我可以改變它來說出我想要的東西嗎? 類似“標簽頁”等的東西
屬性編輯器的右側有一個名為“Misc”的標題。 我也可以改變嗎?
非常感謝你。
一月
____UPDATE____
如果有更好/不同的方式來做我想做的事情,我願意接受所有的建議。 我是新手,到目前為止我所做的工作基於不同網站的各種結果。
我真的希望我的財產出現在設計師中,類似於顯示邊距的方式。 我想要一個可擴展的列表,而不是帶有標簽頁/屬性列表的彈出窗口,每個列表項都是選項卡名稱,后跟字體,然后您可以單擊該字體來編輯字體。 類似於以下內容:
我無法回答更新問題,但我會另外兩個問題:更改文本“成員”:我能看到這樣做的唯一方法是創建一個自定義CollectionEditor來打開自定義CollectionEditor.CollectionForm 。 我沒有試過這個。 停止出現在編輯器中的“Name”屬性:是的,這可以通過過濾結果在TypeConverter.GetProperties方法中完成。 我沒有找到任何使用TypeDescriptor.GetProperties方法的“過濾器”參數,但這可能是因為我沒有正確使用它。 問題是,一旦創建,PropertyDescriptorCollection是只讀的,所以我復制了結果的內容但錯過了我不想要的項目。 這應該工作:
public class TabFontConverter : TypeConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] filter)
{
PropertyDescriptorCollection rawResult = TypeDescriptor.GetProperties(value, filter);
PropertyDescriptor[] arrRawResult = new PropertyDescriptor[rawResult.Count - 1];
int i = 0;
int j = 0;
while (i < rawResult.Count)
{
if (rawResult[i].Name != "Name")
{
arrRawResult[j] = rawResult[i];
j++;
}
i++;
}
PropertyDescriptorCollection filteredResult = new PropertyDescriptorCollection(arrRawResult);
return filteredResult;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.