XAF 字符串属性ComboBox的数据源DataSourcePropertyAttribute提供动态选择的值

1.Code

namespace MemberLevelSecurityDemo.Module
{
    
#region " Copyright (c) 2007 to 2010 Extrasoft Ltd "
    
//
    
//********************************************************************
    
//   The entire contents of this file is protected by UK and       
    
//   International Copyright Laws. Unauthorized reproduction,        
    
//   reverse-engineering, and distribution of all or any portion of  
    
//   the code contained in this file is strictly prohibited and may  
    
//   result in severe civil and criminal penalties and will be       
    
//   prosecuted to the maximum extent possible under the law.        
    
//                                                                   
    
//   RESTRICTIONS                                                    
    
//                                                                   
    
//   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           
    
//   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          
    
//   SECRETS OF EXTRASOFT LTD.                 
    
//                                                                   
    
//   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      
    
//   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        
    
//   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       
    
//   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  
    
//   AND PERMISSION FROM EXTRASOFT LTD.                      
    
//                                                                   
    
//   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       
    
//   ADDITIONAL RESTRICTIONS.       
    
//
    
//***************************************************************************

    
#endregion
    
using System;
    
using System.ComponentModel;
    
using DevExpress.Xpo;
    
using DevExpress.ExpressApp;
    
using DevExpress.ExpressApp.Win.Editors;

    
using DevExpress.XtraEditors;
    
using DevExpress.ExpressApp.Model;
    
using System.Collections;

    
public class WinStringArrayComboPropertyEditor : WinPropertyEditor
    {
        
private string dataSourceProperty = string.Empty;
        
public WinStringArrayComboPropertyEditor(Type objectType, IModelMemberViewItem info)
            : 
base(objectType, info)
        {
            
this.ControlBindingProperty = "EditValue";
            dataSourceProperty 
= info.DataSourceProperty;
        }

        
protected override object CreateControlCore()
        {
            ComboBoxEdit combo 
= CreateControl();
            combo.Properties.QueryPopUp 
+= combo_QueryPopup;
            
return combo;
        }

        
protected override void Dispose(bool disposing)
        {
            ComboBoxEdit combo 
= this.Control as ComboBoxEdit;
            
if (combo != null)
            {
                combo.Properties.QueryPopUp 
-= combo_QueryPopup;
            }
            
base.Dispose(disposing);
        }

        
protected override void OnControlCreated()
        {
            UpdateComboValues();
            
base.OnControlCreated();
        }

        
private ComboBoxEdit CreateControl()
        {
            ComboBoxEdit combo 
= new ComboBoxEdit();
            
// This makes the combo text part non editable
            combo.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            
// Do any more combo setups here
            return combo;
        }

        
private void UpdateComboValues()
        {
            
if (this.CurrentObject != null)
            {
                ComboBoxEdit combo 
= (ComboBoxEdit)this.Control;
                combo.Properties.Items.Clear();
                
try
                {
                    Type t 
= this.CurrentObject.GetType();
                    
//string[] values = (string[])((XPCustomObject)this.CurrentObject).GetMemberValue(this.dataSourceProperty);
                    string[] values = (string[])t.GetProperty(this.dataSourceProperty).GetValue(this.CurrentObject,null);
                    
if (values != null)
                    {
                        combo.Properties.Items.AddRange(values);
                    }
                }
                
catch (Exception exception1)
                {
                    Exception ex 
= exception1;
                    
throw new UserFriendlyException(ex);
                }
            }
        }

        
private void combo_QueryPopup(object sender, CancelEventArgs e)
        {
            UpdateComboValues();
        }

    }

}

2.测试Code

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;

using DevExpress.Xpo;
using DevExpress.Xpo.Metadata;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;

using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

namespace MemberLevelSecurityDemo.Module
{
    [DefaultClassOptions()]
    
public class TestObject : BaseObject
    {
        
public TestObject(Session session)
            : 
base(session)
        {
        }
        
private string _name;
        
public string Name
        {
            
get { return _name; }
            
set { SetPropertyValue("Name"ref _name, value); }
        }
        
private string _type;
        [DataSourceProperty(
"Types")]
        [Custom(
"PropertyEditorType""MemberLevelSecurityDemo.Module.WinStringArrayComboPropertyEditor")]
        [Size(
200)]
        
public string Type
        {
            
get { return _type; }
            
set { SetPropertyValue("Type",ref  _type, value); }
        }

        [Browsable(
false)]
        
public object Types
        {
            
get { return GetTypes(); }
        }
        
/// <summary>
        
/// A trivial method to return a list of strings
        
/// </summary>
        private string[] GetTypes()
        {
            List
<string> memberlist = new List<string>();
            
foreach (XPMemberInfo minfo in this.ClassInfo.Members)
            {
                memberlist.Add(minfo.MemberType.FullName);
            }
            
return memberlist.ToArray();
        }

    }

}

参考:A Win String PropertyEditor with ComboBox supporting DataSourcePropertyAttribute to provide a dynamic selection of values

http://community.devexpress.com/forums/t/81628.aspx


 

原文地址:https://www.cnblogs.com/Tonyyang/p/2052627.html