PropertyGrid無意的發現DisplayNameAttribute及應用


     說到這個winform屬性控件PropertyGrid,要從以前參與項目架構研發,做報表引擎開始,當時我們的目的是想做一個比較簡單的報表引擎沒有RDLC等報表復雜,是為了讓我們的可以操作,用戶可以憑借表單拖拽設置樣式完成報表模板設置,與我們開發人員代碼實現的數據流結合,產生基於apose.cells的excel報表。把我們多而煩的報表業務交給用戶完成,用戶利用報表設計完成的模板保存為xml保存至服務器,工以后使用。當時做到表單控件屬性設計無疑我們采用了PropertyGrid控件,PropertyGrid支持很多的控件設計時交互,很強大,但是對於控件的屬性漢化卻存在問題,當時也沒仔細查閱msdn,直接讓控件屬性為中文字段設計完成,對用戶提示。最后項目基本完成,能夠應對簡單報表,稍微減少了些開發工作量吧。

     在時隔今天做基本控件的封裝的時候突然看見了System.ComponentModel.DisplayNameAttribute這個標簽,見名就考慮是做什么的,查詢msdn才知道他就可以完成對PropertyGrid的現實名稱修改(http://msdn.microsoft.com/zh-cn/library/system.componentmodel.displaynameattribute(v=VS.80).aspx?TPSecNotice)。於是便嘗試了一下。

代碼:

View Code
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace WindowsFormsApplication1 

     class TestControl : System.Windows.Forms.TextBox, Green.SmartUIControls.ISmartUIControl 
    { 
        [System.ComponentModel.Browsable( true)] 
        [System.ComponentModel.DefaultValue( null)] 
        [System.ComponentModel.Description( " 數據綁定匹配屬性 ")] 
        [System.ComponentModel.Category( " Green.SmartUIControl ")] 
        [System.ComponentModel.DisplayName(ControlResource.Data)] 
         public  string Data 
        {  getset; } 

         #region ISmartUIControl 成員 
         private Green.SmartUIControls.IDataBindSetting _DataBindSetting; 
        [System.ComponentModel.Browsable( false)] 
         public Green.SmartUIControls.IDataBindSetting DataBindSetting 
        { 
             get 
            { 
                 if (_DataBindSetting ==  null && ! this.DesignMode) 
                { 
                    _DataBindSetting =  new Green.SmartUIControls.DefaultDataBindSetting( this); 
                } 
                 return _DataBindSetting; 
            } 
             set 
            { 
                 if (value !=  null
                { 
                    _DataBindSetting = value; 
                } 
            } 
        } 
        [System.ComponentModel.Browsable( true)] 
        [System.ComponentModel.DefaultValue( null)] 
        [System.ComponentModel.Description( " 數據綁定匹配屬性 ")] 
        [System.ComponentModel.Category( " Green.SmartUIControl ")] 
        [System.ComponentModel.DisplayName(ControlResource.DataBindProperty)] 
         public  string DataBindProperty 
        { 
             get
             set
        } 

        [System.ComponentModel.Browsable( true)] 
        [System.ComponentModel.DefaultValue( null)] 
        [System.ComponentModel.Description( " Dock ")] 
        [System.ComponentModel.Category( " Green.SmartUIControl ")] 
        [System.ComponentModel.DisplayName(ControlResource.Dock)] 
         public  override System.Windows.Forms.DockStyle Dock 
        { 
             get 
            { 
                 return  base.Dock; 
            } 
             set 
            { 
                 base.Dock = value; 
            } 
        } 
         #endregion 
    } 

     public  class ControlResource 
    { 
#if Debug 
         public  const  string Dock =  " Dock "
         public  const  string DataBindProperty =  " DataBindProperty "
         public  const  string Data =  " Data "
#else 

         public  const  string Dock =  " 停靠 "
         public  const  string DataBindProperty =  " 數據綁定匹配屬性 "
         public  const  string Data =  " 數據 "
#endif 
    } 
}

    最后需要特別說明的是對於Attribute我們只能傳入常量。在我們的很多開發員使用控件等時候我們也許都習慣了英文對於中文不適應了,但是我們可以利用vs的條件編譯繞過,編譯出不同的dll包,開發版和用戶使用版本。如上面的對於調試和發布版的顯示設置。這個ControlResource我們可以開發一個簡單的工具對其xml保存並生成我們需要的代碼維護。

看圖:

設置Dock=Top:

QQ截圖未命名2

設置Dock=Bottom:

QQ截圖未命名

    同時我也考慮到在我們的工作流自定義表單設計和代碼生成工具等中我們也可以運用,表單設計的控件字段屬性設置,保存為xml或者二級制,xaml存儲之類。簡單說一句對於silverlight,wpf 的對於我覺得保存為xaml是最簡單的,我們可以直接保存xaml,並簡單轉換加入父容器中。


免責聲明!

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



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