0

我有一个像 WatermarkPasswordBox 一样工作的用户控件,我想为我的用户控件的 PasswordBox 的 Key Up 事件添加 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 的 PasswordBox 的按键事件。

谢谢。

4

1 回答 1

0
public event KeyEventHandler RelayedKeyUp
{
    add
    {
        passwordB.KeyUp += value;
    }
    remove
    {
        passwordB.KeyUp -= value;
    }
}
于 2013-07-19T16:18:38.250 回答