也许我来晚了,但是如果:
- 你的控件程序集没有在GAC中注册
- 你的控件程序集项目不在你正在工作的解决方案中
那么你可以使用WPF Designer Extensibility。实际上,你可以通过使用属性扩展Visual Studio WPF设计器。
我们需要的属性是ToolboxBrowsableAttribute。这个框架更重要的特性是它允许在一个单独的程序集中(称为设计程序集)为我们的控件定义属性。
所以假设这些是我们的自定义控件:
namespace CustomControls
{
public class CustomTextBox : TextBox
{
}
public class CustomButton : Button
{
}
public class CustomComboBox : ComboBox
{
}
}
自定义控件在名为
CustomControls.dll
的程序集中;为了给每个控件添加所需的属性,需要创建一个名为
CustomControls.Design.dll
的新程序集。这个新程序集必须引用以下内容:
- PresentationCore
- PresentationFramework
- System.Xaml
- WindowsBase
- Microsoft.Windows.Design.Extensibility
- Microsoft.Windows.Design.Interaction
在AssemblyInfo中添加以下代码:
[assembly: ProvideMetadata(typeof(CustomControls.Design.Metadata))]
该属性指出哪个类将为设计师提供属性。
因此,让我们来看看
Metadata
类的代码:
namespace CustomControls.Design
{
public class Metadata : IProvideAttributeTable
{
AttributeTable IProvideAttributeTable.AttributeTable
{
get
{
AttributeTableBuilder builder = new AttributeTableBuilder();
Assembly assembly = Assembly.GetAssembly(typeof(CustomControls.CustomButton));
foreach (Type objectType in assembly.GetTypes())
{
if (objectType.IsPublic && typeof(FrameworkElement).IsAssignableFrom(objectType))
{
builder.AddCustomAttributes(objectType,
ToolboxBrowsableAttribute.No);
}
}
return builder.CreateTable();
}
}
}
}
通过这种方式,我将ToolboxBrowsable
属性添加到扩展FrameworkElement
类的CustomControl
程序集的每个公共对象中,无需逐个装饰每个控件。
CustomControls.Design.dll
必须与CustomControls.dll
位于同一文件夹中。
您可以在这里找到有用的信息(即使该代码涉及略有不同的参数)。
请注意:如果条件1和条件2都未满足,则此方法无效,并且Reza Aghaei的解决方案可能更适合。
希望能对您有所帮助。