C#自定义属性如何显示下拉框?
namespace DataBaseType
{
public partial class Form1 : Form
{
public static string filename = null;
private String Databasetype = "MySql";
[TypeConverter(typeof(DataBaseTypeConverter)), CategoryAttribute("数据库")]
public String DataBaseType
{
get { return Databasetype; }
set { Databasetype = value; }
}
public class DataBaseTypeConverter : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new string[] { "MySql", "Oracle", "ODBC" }); //为啥添加到属性里就不显示下拉框,而缺省是显示的
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return false;
}
}
public Button Button
{
get { return Button1; }
set { Button1 = value; }
}
public Form1()
{
InitializeComponent();
CustomPropertyCollection bt = new CustomPropertyCollection();
bt.Add(new CustomProperty("数据库类型","DataBaseType","数据库类型","数据库类型。",this));//不显示下拉框
//bt.Add(new CustomProperty("数据库类型", "DataBaseType", "数据库", "数据库类型。", this, typeof(CustomDataBaseTypeEditor)));//该如何处理
bt.Add(new CustomProperty("标题", "Text", "数据", "要显示的内容。", Button1));
bt.Add(new CustomProperty("背景色", "BackColor", "外观", "背景色。", Button1));
bt.Add(new CustomProperty("颜色", "ForeColor", "外观", "前景色。", Button1));
propertyGrid1.SelectedObject = bt;
}
private void Button2_Click(object sender, EventArgs e)
{
propertyGrid1.SelectedObject = this; //这里是显示下拉框的
}
}
#region CustomProperty
class CustomDataBaseTypeEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override Object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
ComboBox cb = new ComboBox();
cb.Items.Add("MySql");
cb.Items.Add("Oracle");
cb.Items.Add("ODBC");
//如何弹出窗口让用户选择呢?
return cb.SelectedValue;
}
return value;
}
}
public class CustomProperty
{
#region Private Variables
private string _name = string.Empty;
private object _defaultValue = null;
private object _value = null;
private object _objectSource = null;
private PropertyInfo[] _propertyInfos = null;
#endregion
#region Contructors
public CustomProperty()
{
}
public CustomProperty(string name, string category, string description, object objectSource)
: this(name, name, null, category, description, objectSource, null)
{
}
public CustomProperty(string name, string propertyName, string category, string description, object objectSource)
: this(name, propertyName, null, category, description, objectSource, null)
{
}
public CustomProperty(string name, string propertyName, string category, string description, object objectSource, Type editorType)
: this(name, propertyName, null, category, description, objectSource, editorType)
{
}
public CustomProperty(string name, string propertyName, Type valueType, string category, string description,
object objectSource, Type editorType)
: this(name, new string[] { propertyName }, valueType, null, null, false, true, category, description, objectSource, editorType)
{
}
public CustomProperty(string name, string[] propertyNames, string category, string description, object objectSource)
: this(name, propertyNames, category, description, objectSource, null)
{
}
public CustomProperty(string name, string[] propertyNames, string category, string description, object objectSource, Type editorType)
: this(name, propertyNames, null, category, description, objectSource, editorType)
{
}
public CustomProperty(string name, string[] propertyNames, Type valueType, string category, string description,
object objectSource, Type editorType)
: this(name, propertyNames, valueType, null, null, false, true, category, description, objectSource, editorType)
{
}
public CustomProperty(string name, string[] propertyNames, Type valueType, object defaultValue, object value,
bool isReadOnly, bool isBrowsable, string category, string description, object objectSource, Type editorType)
{
Name = name;
PropertyNames = propertyNames;
ValueType = valueType;
_defaultValue = defaultValue;
_value = value;
IsReadOnly = isReadOnly;
IsBrowsable = isBrowsable;
Category = category;
Description = description;
ObjectSource = objectSource;
EditorType = editorType;
}
#endregion
#region Public Properties
public string Name
{
get { return _name; }
set
{
_name = value;
if (PropertyNames == null)
{
PropertyNames = new string[] { _name };
}
}
}
public string[] PropertyNames { get; set; }
public Type ValueType { get; set; }
public object DefaultValue
{
get { return _defaultValue; }
set
{
_defaultValue = value;
if (_defaultValue != null)
{
if (_value == null) _value = _defaultValue;
if (ValueType == null) ValueType = _defaultValue.GetType();
}
}
}
public object Value
{
get { return _value; }
set
{
_value = value;
OnValueChanged();
}
}
public bool IsReadOnly { get; set; }
public string Description { get; set; }
public string Category { get; set; }
public bool IsBrowsable { get; set; }
public object ObjectSource
{
get { return _objectSource; }
set
{
_objectSource = value;
OnObjectSourceChanged();
}
}
public Type EditorType { get; set; }
#endregion
#region Protected Functions
protected void OnObjectSourceChanged()
{
if (PropertyInfos.Length == 0) return;
object value = PropertyInfos[0].GetValue(_objectSource, null);
if (_defaultValue == null) DefaultValue = value;
_value = value;
}
protected void OnValueChanged()
{
if (_objectSource == null) return;
foreach (PropertyInfo propertyInfo in PropertyInfos)
{
propertyInfo.SetValue(_objectSource, _value, null);
}
}
protected PropertyInfo[] PropertyInfos
{
get
{
if (_propertyInfos == null)
{
Type type = ObjectSource.GetType();
_propertyInfos = new PropertyInfo[PropertyNames.Length];
for (int i = 0; i < PropertyNames.Length; i++)
{
_propertyInfos[i] = type.GetProperty(PropertyNames[i]);
}
}
return _propertyInfos;
}
}
#endregion
#region Prublic Functions
public void ResetValue()
{
Value = DefaultValue;
}
#endregion
}
}