为WPF自定义控件设置属性类别?

11
在WinForms中,我可以在自定义控件属性上添加[Category]属性来指定该属性应包含在哪个属性类别中。在WPF中如何做到这一点?谢谢。
2个回答

18

我发现您不必包含设计时DLL来为自定义控件属性添加[Category]属性。这是一种方法,但实际上,您可以像在WinForms中那样使用任何.NET属性。例如:

/// <summary>
/// The image displayed by the button.
/// </summary>
/// <remarks>The image is specified in XAML as an absolute or relative path.</remarks>
[Description("The image displayed by the button."), Category("Common Properties")] 
public ImageSource Image
{
    get { return (ImageSource)GetValue(ImageProperty); }
    set { SetValue(ImageProperty, value); }
}

3
你需要提供一个“元数据程序集”,也称为“设计时DLL”。这是一个与主程序集同名的程序集,附加了.Design (例如MyCompany.MyControls.Design.dll),并包含实现IRegisterMetadata接口的类。IRegisterMetadata实现将在主程序集中的各个组件的属性表中构建一个表,并将其添加到MetadataStore中。
有关完整信息和示例,请参阅Cider团队的Jim Nakashima的博客文章herehere
有关文档,请参阅MSDN中的WPF Designer Extensibility

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接