TypeConverter 不僅處理型別轉換,還能透過 PropertyTab 將屬性分類到自訂分頁,配合 PropertyDescriptor 實現精細的設計時屬性呈現控制。
[PropertyTab(typeof(MyPropertyTab), PropertyTabScope.Component)]
public class MyControl : WebControl { }
internal class MyPropertyTab : PropertyTab
{
public override string TabName => "我的分頁";
public override Bitmap Bitmap => new Bitmap(typeof(MyPropertyTab), "MyPropertyTab.bmp"); // 16x16 嵌入資源
public override bool CanExtend(object extendee) => extendee is MyControl;
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
{
var ctrl = (MyControl)component;
var list = new ArrayList();
// 動態建立屬性描述器
list.Add(new MyPropertyDescriptor(this, "Property1", typeof(int), ctrl));
// ... 更多屬性
return new PropertyDescriptorCollection((PropertyDescriptor[])list.ToArray(typeof(PropertyDescriptor)));
}
}
private class MyPropertyDescriptor : PropertyDescriptor
{
private MyPropertyTab _owner;
private MyControl _target;
private string _propertyName;
public MyPropertyDescriptor(MyPropertyTab owner, string name, Type type, MyControl target)
: base(name, new Attribute[] { CategoryAttribute.Data, RefreshPropertiesAttribute.All })
{
_owner = owner; _target = target; _propertyName = name;
}
public override Type PropertyType => typeof(int);
public override Type ComponentType => typeof(MyControl);
public override bool IsReadOnly => false;
public override object GetValue(object component) => _target.GetType().GetProperty(_propertyName).GetValue(_target);
public override void SetValue(object component, object value) => _target.GetType().GetProperty(_propertyName).SetValue(_target, value);
public override void ResetValue(object component) { }
public override bool CanResetValue(object component) => false;
public override bool ShouldSerializeValue(object component) => false;
}
| 值 | 說明 |
|---|---|
| Component | 當前元件專用 |
| Document | 當前文件專用 |
| Global | 全域,需父元件顯式移除 |
| Static | 靜態,不可移除 |
All:屬性改變時整個屬性視窗重新整理Repaint:僅重繪屬性格Default:不自動刷新