我在这里有一些概念问题。我知道如何选择 inTextBox
或 in 中的所有文本PasswordBox
。通过GotKeyboardFocus
和PreviewMouseLeftButtonDown
事件,你知道的。这工作正常。
XAML:
PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"
GotKeyboardFocus="SelectAllPassword"
代码隐藏
private void SelectAllPassword(Object sender, RoutedEventArgs e)
{
var pb = (sender as PasswordBox);
if (pb != null)
pb.SelectAll();
}
private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
var pb = (sender as PasswordBox);
if (pb != null)
if (!pb.IsKeyboardFocusWithin)
{
e.Handled = true;
pb.Focus();
}
}
但问题是 - 为什么这不起作用?
XAML:
PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"
代码隐藏:
private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
_txtPassword.SelectAll();
e.Handled = true;
}
在哪里_txtPassword
-TextBox
或PasswordBox
控制。那么为什么我要强制Focus
文本控制呢?