Windows Phone 7/8
当用户输入输入时,我正在尝试将PasswordBox插入符移动到文本末尾,这样他就可以轻松添加新字符。
我找到了这段代码: How can i set the caret position to a specific index in passwordbox in WPF
但我总是在
中点击System.NullReferenceExceptionSetSelection(LoginPasswordBox, 0, 0);
StackTrace
at rodzic.Views.Login.SetSelection(PasswordBox passwordBox, Int32 start, Int32 length)\r\n
at rodzic.Views.Login.ShowPasswordCharsCheckBox_Unchecked(Object sender, RoutedEventArgs e)\r\n
at System.Windows.Controls.Primitives.ToggleButton.OnUnchecked(RoutedEventArgs e)\r\n
at System.Windows.Controls.Primitives.ToggleButton.OnIsCheckedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)\r\n
at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)\r\n
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)\r\n
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)\r\n
at System.Windows.Controls.Primitives.ToggleButton.OnToggle()\r\n
at System.Windows.Controls.Primitives.ToggleButton.OnClick()\r\n
at System.Windows.Controls.Primitives.ButtonBase.<OnMouseLeftButtonUp>b__3()" string
请给我一个提示可能是什么问题或指导我更好地将PasswordBox插入符号移到文本的末尾。
编辑:
XAML
<PasswordBox x:Name="LoginPasswordBox" Password="{Binding Text, Mode=TwoWay, ElementName=PasswordInput}" Template="{StaticResource CustomPasswordBox}"/>
<TextBox x:Name="PasswordInput" Text="{Binding Password, Mode=TwoWay, ElementName=LoginPasswordBox}" Visibility="Collapsed" Style="{StaticResource CustomTextBox}"/>
<CheckBox x:Name="ShowPasswordCharsCheckBox" Content="Show password" Checked="ShowPasswordCharsCheckBox_Checked" Unchecked="ShowPasswordCharsCheckBox_Unchecked" HorizontalAlignment="Left"/>
C#
private void ShowPasswordCharsCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
LoginPasswordBox.Visibility = System.Windows.Visibility.Visible;
PasswordInput.Visibility = System.Windows.Visibility.Collapsed;
// TODO not working :/
SetSelection(LoginPasswordBox, 0, 0);
LoginPasswordBox.Focus();
}
private void SetSelection(PasswordBox passwordBox, int start, int length)
{
passwordBox.GetType().GetMethod("Select", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(passwordBox, new object[] { start, length });
}