簡體   English   中英

反射和自定義ControlDesigner在c#中似乎不起作用

[英]Reflection and custom ControlDesigner doesn't seem to work in c#

我做了一個自定義ControlDesigner,我需要包括和排除屬性網格中顯示的屬性。 但是由於某種原因,似乎只是忽略了代碼? 我不知道我做錯了什么? 我會丟失什么嗎? 我是否需要設置VS或其他功能?

同樣,在示例中,我發現他們似乎對應將remove調用放在何處持不同意見。 在某些示例中,他們在preFilterProperties方法中調用它,在某些示例中,在postFilterProperties()方法中調用它,這使我感到困惑。 在某些示例中,他們在運行base.preFilterProperties()方法之后調用它,有時甚至在調用它之前? 有人可以澄清一下嗎?

這是到目前為止的代碼。 最好是,我還要添加一個屬性,該屬性帶有要排除的屬性名稱列表,但我不知道何時設置這樣的屬性,因為似乎反射是在運行時之前運行的? 還是有什么方法可以做到這一點?

using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using System;
using System.Collections;
using System.Collections.Generic;

namespace PropertyEditorProject
{
    [Designer(typeof(YourClassDesigner))]
    class MyPropertyClass
    {
        protected List<string> _MyList;
        protected string _MyName;
        protected string _MyCarModel;
        protected int _MyAge;

        public List<string> MyList 
        {
            get
            {
                return _MyList;
            }
            set
            {
                _MyList = value;
            }
        }

        public string MyName
        {
            get
            {
                return _MyName;
            }
            set
            {
                _MyName = value;
            }
        }

        public string MyCarModel
        {
            get
            {
                return _MyCarModel;
            }
            set
            {
                _MyCarModel = value;
            }
        }

        public int MyAge
        {
            get
            {
                return _MyAge;
            }
            set
            {
                _MyAge = value;
            }
        }

        public MyPropertyClass()
        {
            _MyList = new List<string>();
            _MyList.Add("Test");
            _MyList.Add("Testing 2");

            _MyName = "TestName";
            _MyCarModel = "Subaru";
            _MyAge = 20;

        }
    }

    internal class YourClassDesigner : ControlDesigner 
    {
        protected override void PreFilterProperties(System.Collections.IDictionary properties)
        {
            properties.Remove("MyAge");
            base.PreFilterProperties(properties);

        }
    }

    static class MainProgram
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Form form = new Form();
            PropertyGrid grid = new PropertyGrid();
            grid.Dock = DockStyle.Fill;
            form.Controls.Add(grid);
            grid.SelectedObject = new MyPropertyClass();
            Application.Run(form);
        }
    }
}

有人有想法么? 感謝您的幫助

經過一番搜索,我找到了我的一個問題的答案:

“遵循的一般規則是添加或刪除“ PreFilter”方法中的項目,並修改“ PostFilter”方法中的現有項目。始終在PreFilter方法中首先調用基本方法,在PostFilter方法中最后調用基本方法這樣可以確保為所有設計師類提供適當的機會來應用他們的更改。”

http://msdn.microsoft.com/en-us/library/ms973820.aspx

我仍然不明白為什么代碼無法運行:(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM