轉自:http://blog.csdn.net/xoyojank/article/details/4322167
PropertyGrid, 做工具一定要用這東西.....
把要編輯的對象看成類的話, 所有要編輯的屬性就是成員
嗯嗯, 最近看了幾眼Ogitor, 它對於PropertyGrid的使用就很不錯
所有要編輯的對象(燈光, 模型, 粒子等等)都有一個共同的基類, 每當選中一個可編輯對象時, 右邊的屬性框里就顯示出當前對象的屬性...(公司那個編輯器要多土就有多土-_-)
盡管Ribbon界面看起來很酷, 我還是對MFC提不起興趣來...
.net里的PropertyGrid更方便, 一點一點來:
屬性自動綁定:
- ref class Human
- {
- public:
- Human()
- {
- this->Name = "(None)";
- this->Age = 0;
- this->IsMale = false;
- }
- property String^ Name;
- property int Age;
- property bool IsMale;
- };
只需要一句
- this->propertyGrid1->SelectedObject = gcnew Human();
它就能自動識別出Human類中的property, 並且自動關聯到PropertyGrid中:
對屬性進行分類並加注釋:
- ref class Human
- {
- public:
- Human()
- {
- this->Name = "(None)";
- this->Age = 0;
- this->IsMale = false;
- this->SkinColor = Color::Yellow;
- }
- [CategoryAttribute("常規"), DescriptionAttribute("名字")]
- property String^ Name;
- [CategoryAttribute("常規"), DescriptionAttribute("年齡")]
- property int Age;
- [CategoryAttribute("外觀"), DescriptionAttribute("性別")]
- property bool IsMale;
- [CategoryAttribute("外觀"), DescriptionAttribute("膚色")]
- property Color SkinColor;
- };
太爽啦~顏色自己就能識別........
弄個Image類型居然還能自己選擇文件...NB啊
除了基本類型之外, Font, Size, Color等復雜類型也可以支持, 那么自定義類型呢?
如果只是像上面那樣放上的話, 只會得到個灰色不可編輯的東西~
要想讓PropertyGrid能夠展開Vector3屬性, 指定一下TypeConverter就可以了:
- [TypeConverterAttribute(ExpandableObjectConverter::typeid)]
- ref struct Vector3
- {
- property float X;
- property float Y;
- property float Z;
- virtual String^ ToString() override
- {
- return String::Format("({0}, {1}, {2})", this->X, this->Y, this->Z);
- }
- };
對於枚舉類型, PropertyGrid會自動顯示成下拉框. 把性別改成枚舉看看:
- enum struct SexType
- {
- Male,
- Female
- };
另外, 還可以彈出自定義的編輯界面, 比如隨時間變化的曲線啦(經常用來做效果...)
這個, 暫時沒需求, 不實現了, 有興趣的參考:Getting the Most Out of the .NET Framework PropertyGrid Control