2010-01-14 52 views

回答

7

可瀏覽意味着一個設計師,像Visual Studio的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框架此屬性的任何使用。

滑稽的邊注:顯然,這也是NET 3.0框架的longest type name

+0

感謝,它只是似乎很奇怪,這取決於把一個屬性上的目標類,當附加屬性的要點是爲了避免必須將「東西」添加到控件中,只是因爲它們可能被用作你的孩子。 – 2010-01-15 09:29:18