2

i declared a class something like this

[DefaultClassOptions]
public class Test:XPObject
{
    Type _classType;

    [NonPersistent]
    public Type ClassType
    {
         get { return _classType; }
         set { SetPropertyValue("ClassType", ref _classType, value); }
    }
}

the problem is, this field is shown as a dropdown list, but i have no control on this list, i can't filter or customize this list. it always opens with all accessible types in all assemblies. neither DataSourceProperty nor DataSourceCriteria attributes worked.

i can do this with other persistent classes, but i can't do with "Type" typed fields.

if there is a workaround please help. thanks in advance.

2
  • It is not clear what you need to achieve. Knowing your goal can help to give you an appropriate solution. By the way, persistent classes in XPO meant to describe the database schema. Possibly, you have chosen a wrong approach to implement a certain functionality.
    – Uranus
    Jul 21, 2014 at 12:02
  • actually this is a non-persistent property which converts given type to a string and maps to a string field of the table of course, thx for this I need to edit my question, but main problem is filtering dropdown list items according to a criteria or custom filtering method, I can't achieve this and need help.
    – Onur
    Jul 30, 2014 at 11:05

2 Answers 2

3

Implement a LocalizedClassInfoTypeConverter descendant, as:

public class LocalizedClassInfoTypeConverter<T> : LocalizedClassInfoTypeConverter {
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
            List<Type> list = new List<Type>();
            foreach (Type t in base.GetStandardValues(context)) {
                if (typeof(T).IsAssignableFrom(t)) list.Add(t);
            }
            return new StandardValuesCollection(list);
        }
    }

Then use it as follows:

[DefaultClassOptions]
public class Test:XPObject
{
    Type _classType;
    [ValueConverter(typeof(TypeToStringConverter))]
    [TypeConverter(typeof(LocalizedClassInfoTypeConverter<MyBaseTypeOrInterface>))]
    public Type ClassType
    {
         get { return _classType; }
         set { SetPropertyValue("ClassType", ref _classType, value); }
    }

}

Source: https://www.devexpress.com/Support/Center/Question/Details/Q364986

3
  • you're the man, thx for your answer, looks useful and I'll try this immediately.
    – Onur
    Jul 30, 2014 at 11:26
  • 1
    hi, your solution worked flawlessly, thank you so much @ErikWitkowski
    – Onur
    Jul 31, 2014 at 10:20
  • 1
    I´m glad I could be useful ;) Aug 1, 2014 at 2:30
2

According to the XAF documentation the DataSourceXXX attributes are supposed to work only for business object types by default, so System.Type cannot be used here. That said, as an alternative solution you can create a non-persistent wrapper POCO and use the DataSourcePropertyAttribute to provide a filtered lookup data source. Find some example code for a similar task at A very interesting way to update and display a persistent property via a non-persistent one

EDIT: Here is also some example code for your particular case:

using System;
using System.Collections.Generic;
using System.Linq;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Xpo;

namespace SimpleProjectManager.Module.BusinessObjects {
    [DomainComponent]
    public class NonPersistentTypeWrapper {
        public string FullName { get; set; }
    }
    [DefaultClassOptions]
    public class PersistentTestObject : BaseObject {
        public PersistentTestObject(Session session) : base(session) { }
        [Persistent("StringColumnNameInDb")]
        private string stringFieldThoseValueIsStoredInDb;

        private NonPersistentTypeWrapper _TypeProperty;
        [NonPersistent, DataSourceProperty("TypesDataSource")]
        public NonPersistentTypeWrapper TypeProperty {
            get {
                if(_TypeProperty == null && !string.IsNullOrEmpty(stringFieldThoseValueIsStoredInDb)) {
                    _TypeProperty = TypesDataSource.Single(v => v.FullName == stringFieldThoseValueIsStoredInDb);
                }
                return _TypeProperty;
            }
            set {
                SetPropertyValue<NonPersistentTypeWrapper>("TypeProperty", ref _TypeProperty, value);
                if(!IsLoading && !IsSaving) {
                    stringFieldThoseValueIsStoredInDb = value != null ? value.FullName : string.Empty;
                    OnChanged("stringFieldThoseValueIsStoredInDb");
                }
            }
        }
        private List<NonPersistentTypeWrapper> _TypesDataSource = null;
        protected IList<NonPersistentTypeWrapper> TypesDataSource {
            get {
                if(_TypesDataSource == null) {
                    _TypesDataSource = new List<NonPersistentTypeWrapper>();
                    foreach(Type type in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()) {
                        if(type.FullName.Contains("Planning")) {// some basic filtering...
                            _TypesDataSource.Add(new NonPersistentTypeWrapper() { FullName = type.FullName });
                        }
                    }
                }
                return _TypesDataSource;
            }
        }
    }
}

and the result: enter image description here While this looks a bit more complex, this approach is universal and can be used for any property type (not only for System.Type) when you need to update a persistent property via a non-persistent one. I hope my solution is now clearer.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.