2

我刚刚遇到AttachedPropertyBrowsableWhenAttributePresentAttribute,但想不出它什么时候有用。有什么理想吗?

4

1 回答 1

7

可浏览意味着设计器(如 Visual Studio 的名为 Cider 的 WPF 设计器)在设计器中显示该属性。由于附加属性不是类型的实际属性,并且可以应用于几乎类型,因此设计人员很难知道何时显示或不显示该属性。这些属性是开发人员让设计人员知道应该在设计人员中显示某个附加属性的一种方式。换句话说:可浏览。这个特定属性让设计者知道这个附加属性应该可以在应用了指定属性的类型上浏览。

附带属性:

public class WhenAttributePresentTestControl : Grid
{
    public static readonly DependencyProperty ShowWhenCustomAttributePresentProperty = DependencyProperty.RegisterAttached(
      "ShowWhenCustomAttributePresent",
      typeof(int),
      typeof(WhenAttributePresentTestControl));

    public static void SetShowWhenCustomAttributePresent(UIElement element, int value)
    {
        element.SetValue(ShowWhenCustomAttributePresentProperty, value);
    }

    [AttachedPropertyBrowsableWhenAttributePresentAttribute(typeof(MyCustomAttribute))]
    public static int GetShowWhenCustomAttributePresent(UIElement element)
    {
        return (int)element.GetValue(ShowWhenCustomAttributePresentProperty);
    }
}

使用示例:

[MyCustomAttribute]
public class CustomLabel : Label
{
}

public class CustomLabelNoCustomAttribute : Label
{
}

设计器将在属性编辑器中为 CustomLabel 显示 ShowWhenCustomAttributePresent 附加属性,但不为 CustomLabelNoCustomAttribute 显示。

来源:http: //blogs.msdn.com/jnak/archive/2008/01/17/showing-attached-properties-in-the-cider-wpf-designer.aspx

实际用法: 我在.Net框架中找不到这个属性的任何用法和Reflector。

有趣的旁注:显然它也是.Net 3.0 框架中最长的类型名称

于 2010-01-14T18:17:03.813 回答