2013-07-19 19 views
0

我有一個像WatermarkPasswordBox一樣工作的用戶控件,我想爲我的用戶控件的PasswordBox的Key Up事件添加KeyUp事件。我該怎麼做?如何爲我的UserControl添加KeyUp事件?

<UserControl 
x:Class="Windows8.StoreApp.Common.CustomControls.WatermarkPasswordTextBox" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:Windows8.StoreApp.Common.CustomControls" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" 
d:DesignWidth="400"> 

<Grid> 
    <PasswordBox x:Name="passwordB" GotFocus="PasswordBox_GotFocus" LostFocus="PasswordBox_LostFocus" PasswordChanged="passwordB_PasswordChanged" Style="{StaticResource AkbankControlStyleWatermarkPasswordBoxLoginFormInputPasswordBox}"></PasswordBox> 
    <TextBlock x:Name="lblWaterMark" Tapped="lblWaterMark_Tapped" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,4,10,4" Opacity="0.8" FontFamily="Segoe UI" FontSize="16" Foreground="#FF8E8E8E" FontWeight="SemiBold"></TextBlock> 
</Grid> 

public WatermarkPasswordTextBox() 
    { 
     this.InitializeComponent(); 
    } 

    private void PasswordBox_GotFocus(object sender, RoutedEventArgs e) 
    { 
     lblWaterMark.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
    } 

    private void PasswordBox_LostFocus(object sender, RoutedEventArgs e) 
    { 
     if ((sender as PasswordBox).Password.Length == 0) 
     { 
      lblWaterMark.Visibility = Windows.UI.Xaml.Visibility.Visible; 
     } 
    } 

    private void passwordB_PasswordChanged(object sender, RoutedEventArgs e) 
    { 
     if ((sender as PasswordBox).Password.Length != 0) 
     { 
      lblWaterMark.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
     } 
    } 

    private void lblWaterMark_Tapped(object sender, TappedRoutedEventArgs e) 
    { 
     lblWaterMark.Visibility = Windows.UI.Xaml.Visibility.Collapsed; 
     passwordB.Focus(Windows.UI.Xaml.FocusState.Pointer); 
    } 

    private string _watermark=String.Empty; 
    public string Watermark 
    { 
     get 
     { 
      _watermark = lblWaterMark.Text; 
      return _watermark; 
     } 
     set 
     { 
      SetProperty<string>(ref _watermark, value, "Watermark"); 
      lblWaterMark.Text = _watermark; 
     } 
    } 

    private int _lenghtMax; 
    public int LenghtMax 
    { 
     get 
     { 
      if (passwordB != null) 
      { 
       _lenghtMax = passwordB.MaxLength; 
       return _lenghtMax; 
      } 
      else 
      { 
       return 0; 
      } 
     } 
     set 
     { 
      if (passwordB != null) 
      { 
       SetProperty<int>(ref _lenghtMax, value, "LenghtMax"); 
       passwordB.MaxLength = _lenghtMax; 
      } 
     } 
    } 


    private string _passText = String.Empty; 
    public string PassText 
    { 
     get 
     { 
      if (passwordB != null) 
      { 
       _passText = passwordB.Password; 
       return _passText; 
      } 
      else 
      { 
       return String.Empty; 
      } 

     } 
     set 
     { 
      if (passwordB != null) 
      { 
       SetProperty<string>(ref _passText, value, "PassText"); 
       passwordB.Password = _passText; 
       passwordB_PasswordChanged(passwordB, null); 
      } 
      else 
      { 
       SetProperty<string>(ref _passText, value, "PassText"); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null) 
    { 
     if (Equals(storage, value)) return false; 

     storage = value; 
     OnPropertyChanged(propertyName); 
     return true; 
    } 

    private void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

我想用這樣的; 並且這個Key_Up將等於mycontrol的密碼箱的按鍵事件。

謝謝。

回答

0
public event KeyEventHandler RelayedKeyUp 
{ 
    add 
    { 
     passwordB.KeyUp += value; 
    } 
    remove 
    { 
     passwordB.KeyUp -= value; 
    } 
} 
+0

感謝萬@filip Skakun! 你知道我該如何讓我的自定義屬性雙向綁定? –

+0

它需要是一個在DependencyObject上定義的依賴項屬性。 –

+0

我該怎麼做@filip Skakun?你能解釋給我嗎? –