这是一个 UserControl 的简化示例,只有 3 个属性:LookupType、RemitToAddr(仅适用于 LookupType.Vendor)、ShipMethID(仅适用于 LookupType.ShipMeth)。一、LookupControl:
public enum LookupType
{
Vendor,
ShipMeth
}
[TypeConverter(typeof(LookupControlConverter))]
public partial class LookupControl : UserControl
{
private LookupType lookupType;
public LookupControl()
{
InitializeComponent();
}
[RefreshProperties(RefreshProperties.All)]
[Browsable(true)]
public LookupType LookupType
{
get => lookupType;
set
{
lookupType = value;
// Clear out unwanted property values
switch (value)
{
case LookupType.Vendor:
ShipMethID = null;
break;
case LookupType.ShipMeth:
RemitToAddr = null;
break;
}
}
}
[Browsable(true)]
public string RemitToAddr { get; set; }
[Browsable(true)]
public string ShipMethID { get; set; }
}
LookupControl 在LookupTypeproperty 设置器中包含一些逻辑,用于清除对当前 LookupType 没有意义的属性值。这可确保您的控件不能有不一致的属性值。
对于 Windows 窗体设计器中的 PropertyGrid,我实现了 TypeConverter:
internal sealed class LookupControlConverter : TypeConverter
{
private readonly PropertyDescriptorCollection allProps = TypeDescriptor.GetProperties(typeof(LookupControl), new[] { BrowsableAttribute.Yes });
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
if (context?.Instance is LookupControl)
return true;
return base.GetPropertiesSupported(context);
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
if (context.Instance is LookupControl control)
{
var propList = allProps.Cast<PropertyDescriptor>().ToList();
// Remove the unwanted property descriptors based on the current value of LookupType
switch (control.LookupType)
{
case LookupType.Vendor:
propList.Remove(allProps[nameof(LookupControl.ShipMethID)]);
break;
case LookupType.ShipMeth:
propList.Remove(allProps[nameof(LookupControl.RemitToAddr)]);
break;
}
return new PropertyDescriptorCollection(propList.ToArray(), true);
}
return allProps;
}
}
注意:要使LookupControlConverter生效,您需要在用户控件上应用[TypeConverter(typeof(LookupControlConverter))]属性并在属性上应用[RefreshProperties(RefreshProperties.All)]
LookupType。后者告诉设计者,如果属性发生变化,它应该重新查询所有属性(不仅仅是值)。然后 TypeConverter 只提供匹配的属性。
Visual Studio 中的 IntelliSense 没有解决方案,但如果你实现如上所示的属性,至少你可以保持属性值一致。
最后一点:Visual Studio 缓存了设计器中使用的控件。因此,如果您进行更改,您可能需要清理解决方案并重新启动 VS 以使更改生效。