2011-04-21 22 views
2

我是新的wpf。我目前正在開發自定義控件,並且我希望從xaml嚴格定義某些屬性。我想要實現的示例是Effect屬性,它僅顯示「必須在XAML中設置值」文本。有人可以告訴我該怎麼做,或者我應該使用哪個屬性?在此先感謝在Visual Studio屬性面板上禁用屬性

回答

0

您需要扶養性能

http://msdn.microsoft.com/en-us/library/ms752914.aspx

public static readonly DependencyProperty IsSpinningProperty = 
    DependencyProperty.Register(
    "IsSpinning", typeof(Boolean),...); 

public bool IsSpinning 
{ 
    get { return (bool)GetValue(IsSpinningProperty); } 
    set { SetValue(IsSpinningProperty, value); } 
} 

或只是效果添加到上你的新控件的XAML:如果你想隱藏的屬性

<local:NewControl> 
    <local:NewControl.Effect> 
     <DropShadowEffect/> 
    </local:NewControl.Effect> 
</local:NewControl> 
+0

我正在使用DependencyProperty。我想實現的目的是讓用戶不能通過Visual Studio屬性面板編輯它的值。 – Araym 2011-04-21 09:52:36

1

屬性面板,你可以使用BrowsableAttribute實現它:

BrowsableAttribute Class

可視化設計器通常在屬性窗口中顯示那些沒有可瀏覽屬性或者用值爲true的BrowsableAttribute構造函數標記的成員。這些成員可以在設計時進行修改。 標記爲值爲false的BrowsableAttribute構造函數的成員不適合進行設計時編輯,因此不會顯示在可視化設計器中。默認值是true。

[Browsable(false)] 
public int HiddenProperty { 
    get { 
     // Insert code here. 
     return 0; 
    } 
    set { 
     // Insert code here. 
    } 
} 
+0

將Browsable設置爲false將使其完全隱藏,這意味着在使用設計器時,用戶可能不知道它存在。我想讓它仍然顯示在屬性面板上,但用戶必須輸入XAML代碼才能設置值。 – Araym 2011-04-21 09:55:43

+0

好的。然後,您需要使用另一個屬性來更改將在「屬性面板」中編輯屬性時要使用的設計器。我過去使用過它,但現在我無法訪問該代碼庫並找到它的名稱。 – StanislawSwierc 2011-04-21 10:08:49

+0

我無法在代碼中檢查它,但據我所知,TypeDescriptionProviderAttribute和CustomTypeDescriptor是組合方式。 – StanislawSwierc 2011-05-02 22:20:18