wpf控件设计时支持(1)2011-12-21 博客园 Clingingboy1.属性元数据

在vs IDE中,在asp.net,winfrom等开发环境下,右侧的PropertyGrid属性面板,会对属性进行分类,这有利于了解控件属性的用途.若你之前在.net平台下做过控件开发,你应该知道这些功能是通过属性元数据实现的,比如使用Category元数据,把Content属性分到Content类别下.
[Category("Content")]public object Content { get; set; }
这种方法在开发wpf自定义控件依然可用.我们以名为DesginCustomControl的自定义控件为例子.因为是wpf控件,所以属性变更为依赖项属性.
public class DesginCustomControl : ContentControl{ [Category("Content")] public string Content { get { return (string)GetValue(ContentProperty); } set { SetValue(ContentProperty, value); } } public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(string), typeof(DesginCustomControl), new UIPropertyMetadata(String.Empty)); }
效果如下: