Search code examples
devexpressxafxpo

Devexpress XAF class property datasource filtering


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.


Solution

  • 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