Using ITypeDiscoveryService within the SmartTag

Each web control offers a SmartTag. It’s that little arrow on the right edge of the control and pops up a list of commands. Here’s how to add a command where the user can pick from a list of types. This is an extension of the Using ITypeDiscoveryService in a TypeConverter post.

The idea is very easy.

1. Create a TypeConverter that implements GetStandardValues().

2. Create a command in the SmartMenu for the Type property. It should be of type “string”.

3. Assign the TypeConverter to that property.

The SmartTag will build a dropdownlist using your TypeConverter.

Here’s how I implemented the POCOTypeName property found in the POCODataSource control of Versatile DataSources.

TypeConverter

The new type converter is based on BaseTypeNameTypeConverter from the earlier post.

public class POCOTypeNameTypeConverter : BaseTypeNameTypeConverter
{
public override bool SupportedType(Type pTypeToEvaluate)
{
if (typeof(DataContext.IAutoRegisterWithPOCODataContext).IsAssignableFrom(pTypeToEvaluate))
return true;
if (typeof(System.Web.UI.Control).IsAssignableFrom(pTypeToEvaluate))
return false;

// eliminate DataContext, Entity, and EntityDAO classes
if (Attribute.GetCustomAttributes(pTypeToEvaluate, typeof(EntityDAOTypeAttribute), true).Length > 0)
return false;

if (Attribute.GetCustomAttributes(pTypeToEvaluate, typeof(EntityDAODataSourceDefaultsAttribute), true).Length > 0)
return false;

if (typeof(DataContext.IEntityFactoryDataContext).IsAssignableFrom(pTypeToEvaluate))
return false;

if (pTypeToEvaluate.Name == "global_asax")
return false;

return true;
}
}

ActionList class for SmartTag

public class POCODataSourceDesignerActionList : DesignerActionList
{
protected System.Web.UI.Control fControl = null;

public POCODataSourceDesignerActionList(POCODataSource pComponent)
: base(pComponent)
{
fControl = (System.Web.UI.Control)pComponent;
}

public override DesignerActionItemCollection GetSortedActionItems()
{
DesignerActionItemCollection vItems = new DesignerActionItemCollection();

vItems.Add(new DesignerActionPropertyItem("POCOTypeName",
"Full name of the POCO class", "",
"Provides the name of a type that represents the POCO class."));
return vItems;
}

[TypeConverter(typeof(Designer.POCOTypeNameTypeConverter))]
public virtual string POCOTypeName
{
get
{
return ((POCODataSource)fControl).POCOTypeName;
}
set
{
TypeDescriptor.GetProperties(fControl)["POCOTypeName"].SetValue(fControl, value);
}
}
}

ControlDesigner class

public class POCODataSourceDesigner : System.Web.UI.Design.ControlDesigner
{
public override DesignerActionListCollection ActionLists
{
get
{
if (null == fActionLists)
{
fActionLists = new DesignerActionListCollection();
fActionLists.AddRange(base.ActionLists);

fActionLists.Add(new POCODataSourceDesignerActionList((POCODataSource)this.Component));
}
return fActionLists;
}
}
private DesignerActionListCollection fActionLists;
}

For more on SmartTags, see Designer Commands and the DesignerAction Object Model for Windows Forms

No Comments