【问题标题】:Set property category for WPF custom control?为 WPF 自定义控件设置属性类别?
【发布时间】:2010-01-01 18:23:16
【问题描述】:

在 WinForms 中,我可以将 [Category] ​​属性添加到自定义控件属性,以指定应包含该属性的属性类别。我如何在 WPF 中做到这一点?谢谢

【问题讨论】:

    标签: wpf wpf-controls custom-controls


    【解决方案1】:

    我发现您没有必须包含设计时 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); }
    }
    

    【讨论】:

      【解决方案2】:

      您需要提供“元数据程序集”,也称为“设计时 DLL”。这是一个与主程序集同名的程序集,附加了 .Design(例如 MyCompany.MyControls.Design.dll),并包含一个实现 IRegisterMetadata 的类。 IRegisterMetadata 实现为您的主程序集中的各种组件构建一个属性表,并将其添加到 MetadataStore。

      有关完整信息和示例,请参阅 Cider 团队 herehere 的 Jim Nakashima 的博客文章。

      有关文档,请参阅 MSDN 中的 WPF Designer Extensibility

      【讨论】:

        猜你喜欢
        • 2013-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多