2011-10-07 80 views
1

對新問題感到抱歉,但對於我的舊問題,我找到了解決辦法。我想要一個bindablePasswordBox並找到this示例。現在我正在嘗試將此代碼集成到我的項目中。它像我想要的那樣工作,但還有一個問題。因爲我想我BindablePasswordBox的風格基地的其他控制我必須把它從一個裝飾改變像例如,爲了控制像這裏的代碼:裝修工改爲控制

public class BindablePasswordBox : Control 
    { 
     public static readonly DependencyProperty PasswordProperty; 

     private bool isPreventCallback; 
     private RoutedEventHandler savedCallback; 

     static BindablePasswordBox() 
     { 
      PasswordProperty = DependencyProperty.Register(
       "Password", 
       typeof(string), 
       typeof(BindablePasswordBox), 
       new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnPasswordPropertyChanged)) 
      ); 
     } 

     public BindablePasswordBox() 
     { 
      savedCallback = HandlePasswordChanged; 

      PasswordBox passwordBox = new PasswordBox(); 
      passwordBox.PasswordChanged += savedCallback; 
      **Child** = passwordBox; 
     } 

     public string Password 
     { 
      get { return GetValue(PasswordProperty) as string; } 
      set { SetValue(PasswordProperty, value); } 
     } 

     private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs eventArgs) 
     { 
      BindablePasswordBox customPasswordBox = (BindablePasswordBox)d; 
      PasswordBox passwordBox = (PasswordBox)customPasswordBox.**Child**; 

      if (customPasswordBox.isPreventCallback) 
      { 
       return; 
      } 

      passwordBox.PasswordChanged -= customPasswordBox.savedCallback; 
      passwordBox.Password = (eventArgs.NewValue != null) ? eventArgs.NewValue.ToString() : ""; 
      passwordBox.PasswordChanged += customPasswordBox.savedCallback; 
     } 

     private void HandlePasswordChanged(object sender, RoutedEventArgs eventArgs) 
     { 
      PasswordBox passwordBox = (PasswordBox)sender; 

      isPreventCallback = true; 
      Password = passwordBox.Password; 
      isPreventCallback = false; 
     } 
    } 
} 

我得到視覺強調了兩個錯誤工作室。我用粗體字標記了這兩個位置。 「孩子」都被標記爲錯誤。

請幫助我,給我一個提示來解決這個問題。我是wpf的新手,所以請幫助我。

塞波

+0

'Control'沒有'Child'。它有'Content'。 –

+0

您的意思是說,我只是可以將內容更改爲Child?這是行不通的:( – user980509

+0

對不起,錯了,你可以將你的基類從'Control'改爲'ContentControl',這樣'Content'就可以使用 –

回答

0

Child僅定義爲Decorator一部分。

+0

你知道一個解決方法嗎,所以bindablePasswordBox也是這樣做的但只是一個控制,沒有裝飾者? – user980509