2013-03-22 106 views
1

我想添加自定義屬性我的用戶控制,使得顯示(或開啓/關閉)的各種選項自定義在WPF用戶控件屬性

<toolkit:UC_TitleBar title="My Application Title" showCloseButton="false" /> 
當用戶控件將然後使用該屬性

我該怎麼做?

回答

2

你需要的是什麼dependency properties

public class UC_TitleBar : UserControl 
{ 
    public static readonly DependencyProperty ShowCloseButtonProperty = DependencyProperty.Register("ShowCloseButton", 
                typeof(Boolean), typeof(UC_TitleBar), new FrameworkPropertyMetadata(false)); 
    public bool ShowCloseButton 
    { 
     get { return (bool)GetValue(ShowCloseButtonProperty); } 
     set { SetValue(ShowCloseButtonProperty, value); } 
    } 
} 
0
//add dependency property 
    public static DependencyProperty MyTestProperty; 

    //init dependency property in static control constructor 
    static MyControl() 
    { 
     var myTestPropertyMetadata = new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender, MyTestPropertyChanged); 


       MyTestProperty= DependencyProperty.Register("MyTest", 
                 typeof(string), 
                 typeof(MyControl), 
                 myTestPropertyMetadata); 
    } 

    //implement property 
    public String MyTest 
    { 
     get { return (String)GetValue(MyTestProperty); } 
     set 
     { 
      SetValue(MyTestProperty, value); 
     } 
    } 

    //using in xaml 
    <MyControls:MyControl MyTest="dfdsf" /> 

在MSDN

閱讀更多關於依賴屬性