2017-08-01 58 views
1

將wpf自定義控件添加到工具箱並拖至MainWindow時,XAML編輯器中自動生成的文本包含一些properties = values默認。將自定義控件從工具箱拖放到主窗口時,更改XAML自動生成文本

如何更改此文本,以便自動包含我的自定義控件的某些新屬性和/或刪除其他屬性?

+0

你誤解:當一個WPF控件拖到從工具箱中的窗口,在XAML編輯器自動生成的文本包含一些屬性=值*不等於默認值*。什麼是重複默認值? – ASh

回答

0

您可以通過組合System.ComponentModel屬性和DependencyProperty元數據獲得相當靈活的設計時間行爲。該PropertyMetadata類有一個構造函數的默認值:

[Category("MyCustomCategory")] 
public string MyCustomProperty 
{ 
    get { return GetValue(MyCustomPropertyProperty).ToString(); } 
    set { SetValue(MyCustomPropertyProperty, value); } 
} 
public static DependencyProperty MyCustomPropertyProperty = 
    DependencyProperty 
    .Register(
     "MyCustomProperty", 
     typeof(string), 
     typeof(MyCustomUserControl), 
     new PropertyMetadata("My default value")); // <--- default value 

enter image description here

相關問題