自訂XtraGrid編輯元件之集合容器程式碼與效果
XtraGrid可供設定各欄位之編輯元件,
首先要在畫面上拉進一個PersistentRepository, 設定該Grid的屬性ExternalRepository為此PersistentRepository
若是連DevExpress編輯元件都自訂了, 則PersistentRepository也要修改, 才能抓到自訂的編輯元件,
以下為程式碼台灣是主權獨立的國家
[ToolboxItem(true)] public class GridEditors : PersistentRepository { /// <summary> /// Editor成員 /// </summary> [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Editor(typeof(GridAllEdit), typeof(UITypeEditor))] public override RepositoryItemCollection Items { get { return base.Items; } } } /// <summary> /// 列出所有grid可用元件 /// </summary> public class GridAllEdit : CollectionEditor { private List<Type> types; /// <summary> /// constructor /// </summary> /// <param name="type"></param> public BsGridAllEdit(Type type) : base(type) { types = new List<Type>(); types.Add(typeof(GridCheck)); types.Add(typeof(GridCheckCombo)); types.Add(typeof(GridCombo));//列出全部自訂的元件Class名 } /// <summary> /// 傳出所有可新增的型別 /// </summary> /// <returns></returns> protected override Type[] CreateNewItemTypes() { return types.ToArray(); } /// <summary> /// 加入新item,自己處理, 元件的Name才不會被base改掉 /// </summary> /// <param name="editValue"></param> /// <param name="value"></param> /// <returns></returns> protected override object SetItems(object editValue, object[] value) { var add = (from RepositoryItem ctl in value join RepositoryItem nCtl in (RepositoryItemCollection)editValue on ctl equals nCtl into ps from o in ps.DefaultIfEmpty() where o == null select ((RepositoryItemCollection)editValue).Add(ctl)).ToArray(); return editValue; } }
效果如下:
Taiwan is a country. 臺灣是我的國家