IMPORTANT SAFETY TIP: This code works for me, but hasn't been extensively tested. Your mileage may vary.
For this sort of thing, you need to create your own custom property editor that implements a combo box populated with the list of values that you want to choose between. You can then use the model editor to assign this property editor directly to the property that you want to use it for.
Here is an example of what I did for one of my web projects:
abstract class WebComboBoxPropertyEditorBase : ASPxPropertyEditor
{
protected abstract bool FirstItemIsNull { get; }
protected abstract Type EditorValueType { get; }
protected abstract void PopulateEditorItems(ListEditItemCollection items);
protected virtual object ConvertValueToEditorValue(object value)
{
return value;
}
protected virtual object ConvertEditorValueToValue(object editorValue)
{
return editorValue;
}
protected override void SetupControl(WebControl control)
{
base.SetupControl(control);
if (control is ASPxComboBox)
{
ASPxComboBox editor = (ASPxComboBox)control;
editor.AutoPostBack = ImmediatePostData;
editor.SelectedIndexChanged += new EventHandler(ExtendedEditValueChangedHandler);
editor.Items.Clear();
editor.ValueType = EditorValueType;
PopulateEditorItems(editor.Items);
}
}
protected override WebControl CreateEditModeControlCore()
{
return new ASPxComboBox();
}
protected override object GetControlValueCore()
{
return ConvertEditorValueToValue(Editor.SelectedIndex < (FirstItemIsNull ? 1 : 0) ? null : Editor.SelectedItem.Value);
}
protected override void ReadEditModeValueCore()
{
object value = ConvertValueToEditorValue(PropertyValue);
Editor.SelectedIndex =
value == null ? (FirstItemIsNull ? 0 : -1) : Editor.Items.FindByValue(value).Index;
}
protected override string GetPropertyDisplayValue()
{
if (PropertyValue == null)
return "";
else
return PropertyValue.ToString();
}
public WebComboBoxPropertyEditorBase(Type objectType, DictionaryNode info)
: base(objectType, info)
{
}
public override void BreakLinksToControl(bool unwireEventsOnly)
{
if (Editor != null)
{
Editor.SelectedIndexChanged -= new EventHandler(ExtendedEditValueChangedHandler);
}
base.BreakLinksToControl(unwireEventsOnly);
}
public new ASPxComboBox Editor
{
get { return (ASPxComboBox)base.Editor; }
}
}
// We don't want the property editor to be automatically assigned, so don't use this attribute
//[PropertyEditor(typeof(int))]
class WebPointsPropertyEditor : WebComboBoxPropertyEditorBase
{
protected override bool FirstItemIsNull { get { return true; } }
protected override Type EditorValueType { get { return typeof(int); } }
protected override void PopulateEditorItems(ListEditItemCollection items)
{
items.Add("", null);
for (int i = 1; i < 100; i++)
items.Add(i.ToString(), i);
}
public WebPointsPropertyEditor(Type objectType, DictionaryNode info)
: base(objectType, info)
{
}
}
Note: to use Enums as values, set the EditorValueType to typeof(int), and override the following two functions:
protected override object ConvertValueToEditorValue(object value)
{
return (int)value;
}
protected override object ConvertEditorValueToValue(object editorValue)
{
return editorValue == null ? InsertCorrespondingEnumValueHere : (InsertCorrespondingEnumTypeHere)editorValue;
}
Something about the combo box seems to require EditorValueTypes that can be implicitly converted to/from strings and to/from the actual enum type that you want to use.