0

WatermarkPasswordBox처럼 작동하는 사용자 컨트롤이 있고 사용자 컨트롤의 PasswordBox 키 업 이벤트에 KeyUp 이벤트를 추가하고 싶습니다. 내가 어떻게 해?UserControl에 KeyUp 이벤트를 추가하려면 어떻게해야합니까?

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)); 
     } 
    } 

<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> 

나는 이런 식으로 사용하려면; 이고이 Key_Up은 mycontrol의 PasswordBox 키 업 이벤트와 같습니다.

감사합니다.

답변

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

감사합니다. @Filip Skakun! 사용자 정의 속성 양방향 바인딩 가능을 어떻게 만들 수 있습니까? –

+0

DependencyObject에 정의 된 종속성 속성이어야합니다. –

+0

어떻게 Skakun을 @filip 할 수 있습니까? 설명해 주시겠습니까? –

관련 문제