強大的PropertyGrid


轉自:http://blog.csdn.net/xoyojank/article/details/4322167

 PropertyGrid, 做工具一定要用這東西.....

把要編輯的對象看成類的話, 所有要編輯的屬性就是成員

嗯嗯, 最近看了幾眼Ogitor, 它對於PropertyGrid的使用就很不錯

所有要編輯的對象(燈光, 模型, 粒子等等)都有一個共同的基類, 每當選中一個可編輯對象時, 右邊的屬性框里就顯示出當前對象的屬性...(公司那個編輯器要多土就有多土-_-)

盡管Ribbon界面看起來很酷, 我還是對MFC提不起興趣來...

.net里的PropertyGrid更方便, 一點一點來:

屬性自動綁定:

 

[cpp]  view plain copy
  1. ref class Human  
  2. {  
  3. public:  
  4.     Human()  
  5.     {  
  6.         this->Name = "(None)";  
  7.         this->Age = 0;  
  8.         this->IsMale = false;  
  9.     }  
  10.   
  11.     property String^ Name;  
  12.     property int Age;  
  13.     property bool IsMale;  
  14. };  

 

只需要一句

 

[cpp]  view plain copy
  1. this->propertyGrid1->SelectedObject = gcnew Human();  

 

它就能自動識別出Human類中的property, 並且自動關聯到PropertyGrid中:

對屬性進行分類並加注釋:

 

[cpp]  view plain copy
  1. ref class Human  
  2. {  
  3. public:  
  4.     Human()  
  5.     {  
  6.         this->Name = "(None)";  
  7.         this->Age = 0;  
  8.         this->IsMale = false;  
  9.         this->SkinColor = Color::Yellow;  
  10.     }  
  11.   
  12.     [CategoryAttribute("常規"), DescriptionAttribute("名字")]  
  13.     property String^ Name;  
  14.     [CategoryAttribute("常規"), DescriptionAttribute("年齡")]  
  15.     property int Age;  
  16.     [CategoryAttribute("外觀"), DescriptionAttribute("性別")]  
  17.     property bool IsMale;  
  18.     [CategoryAttribute("外觀"), DescriptionAttribute("膚色")]  
  19.     property Color SkinColor;  
  20. };  

 

太爽啦~顏色自己就能識別........

弄個Image類型居然還能自己選擇文件...NB啊

除了基本類型之外, Font, Size, Color等復雜類型也可以支持, 那么自定義類型呢?

如果只是像上面那樣放上的話, 只會得到個灰色不可編輯的東西~

要想讓PropertyGrid能夠展開Vector3屬性, 指定一下TypeConverter就可以了:

 

[cpp]  view plain copy
  1. [TypeConverterAttribute(ExpandableObjectConverter::typeid)]  
  2. ref struct Vector3  
  3. {  
  4.     property float X;  
  5.     property float Y;  
  6.     property float Z;  
  7.   
  8.     virtual String^ ToString() override  
  9.     {  
  10.         return String::Format("({0}, {1}, {2})"this->X, this->Y, this->Z);  
  11.     }  
  12. };  

 

對於枚舉類型, PropertyGrid會自動顯示成下拉框. 把性別改成枚舉看看:

 

[cpp]  view plain copy
  1. enum struct SexType  
  2. {  
  3.     Male,  
  4.     Female  
  5. };  

 

另外, 還可以彈出自定義的編輯界面, 比如隨時間變化的曲線啦(經常用來做效果...)

這個, 暫時沒需求, 不實現了, 有興趣的參考:Getting the Most Out of the .NET Framework PropertyGrid Control


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM