.NET 學習

.NET 學習生活感想... 万事成蹉跎..... 贵在坚持 及时整理自己做过和学过的东西

博客园 首页 新随笔 联系 订阅 管理

http://www.devexpress.com/Support/Center/e/E689.aspx

 

 
using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.ExpressApp.Utils;
using DevExpress.ExpressApp.Model;
using DevExpress.ExpressApp.Editors;
using DevExpress.XtraEditors.Repository;

namespace WinSolution.Module.Win {
    [PropertyEditor(
typeof(System.Enum), true)]
    
public class EnumPropertyEditorEx : DevExpress.ExpressApp.Win.Editors.EnumPropertyEditor {
        
public EnumPropertyEditorEx(Type objectType, IModelMemberViewItem model)
            : 
base(objectType, model) {
        }
        
private bool TypeHasFlagsAttribute() {
            
return GetUnderlyingType().GetCustomAttributes(typeof(FlagsAttribute), true).Length > 0;
        }
        
protected override object CreateControlCore() {
            CheckedComboBoxEdit checkedEdit 
= new CheckedComboBoxEdit();
            checkedEdit.TextChanged 
+= checkedEdit_TextChanged;
            
if (TypeHasFlagsAttribute())
                
return checkedEdit;
            
return base.CreateControlCore();
        }
        
//Dennis: this code is needed to bypass a binding problem in the XtraEditors (S35490)
        private void checkedEdit_TextChanged(object sender, EventArgs e) {
            ((CheckedComboBoxEdit)sender).RefreshEditValue();
        }
        
protected override RepositoryItem CreateRepositoryItem() {
            
if (TypeHasFlagsAttribute())
                
return new RepositoryItemCheckedComboBoxEdit();
            
return base.CreateRepositoryItem();
        }
        
protected override void SetupRepositoryItem(RepositoryItem item) {
            
base.SetupRepositoryItem(item);
            
if (TypeHasFlagsAttribute()) {
                EnumDescriptor descriptor 
= new EnumDescriptor(GetUnderlyingType());
                RepositoryItemCheckedComboBoxEdit checkedItem 
= ((RepositoryItemCheckedComboBoxEdit)item);
                checkedItem.BeginUpdate();
                checkedItem.Items.Clear();
                checkedItem.SelectAllItemVisible 
= false;
                
//Dennis: this is required to show localized items in the editor.
                foreach (object value in descriptor.Values)
                    checkedItem.Items.Add(value, descriptor.GetCaption(value), CheckState.Unchecked, 
true);
                
//Dennis: use this method if you don't to show localized items in the editor.
                
//checkedItem.SetFlags(GetUnderlyingType());
                checkedItem.EndUpdate();
            }
        }
    }
}

 

 

posted on 2010-08-18 11:42  Tonyyang  阅读(1002)  评论(1编辑  收藏  举报
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/